[ABC317G] Rearranging
Problem Statement
There is a grid with rows and columns. The square at the -th row from the top and the -th column from the left contains the integer .
Here, the squares contain occurrences of each of , for a total of integers.
You perform the following operations to swap the numbers written on the squares.
- For in this order, do the following.
- Freely rearrange the numbers written in the -th row. That is, freely choose a permutation of , and replace with simultaneously.
Your goal is to perform the operations so that each column contains each of once. Determine if this is possible, and if so, print such a resulting grid.
盲猜没有无解。
一列一列处理,每次算出一列。而某一列可以建出二分图计算。如果第 行存在数 就从左部 向右部 连一条边,问题变为找一个匹配,匈牙利即可(为什么大家都Dinic 的)
现在要证明不存在无解情况,也就是不存在某一时刻二分图不存在完美匹配,转成Hall 定理的话,某 行出现过的不同的数一定超过 ,设剩下 列没处理,那么每种数最多出现 次, 行中共有 个数,所以至少会有 个不同的数,二分图一定存在完美匹配。
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
int n,m,a,b,c,vs[N],st[N],tp,idx,dfn[N],low[N];
struct graph{
int hd[N],e_num;
struct edge{
int v,nxt;
}e[N<<1];
void add_edge(int u,int v)
{
e[++e_num]=(edge){v,hd[u]};
hd[u]=e_num;
e[++e_num]=(edge){u,hd[v]};
hd[v]=e_num;
}
}g,h;
int read()
{
int s=0;
char ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
while(ch>='0'&&ch<='9')
s=s*10+ch-48,ch=getchar();
return s;
}
void tarjan(int x)
{
dfn[x]=low[x]=++idx;
st[++tp]=x;
for(int i=g.hd[x];i;i=g.e[i].nxt)
{
int v=g.e[i].v;
if(!dfn[v])
{
tarjan(v);
low[x]=min(low[x],low[v]);
if(low[v]==dfn[x])
{
++n;
while(st[tp]^v)
h.add_edge(n,st[tp--]);
h.add_edge(n,st[tp--]);
h.add_edge(x,n);
}
}
else
low[x]=min(low[x],dfn[v]);
}
}
void dfs(int x,int y)
{
st[++tp]=x;
if(x==c)
{
for(int i=1;i<=tp;i++)
vs[st[i]]=1;
return;
}
for(int i=h.hd[x];i;i=h.e[i].nxt)
if(h.e[i].v^y)
dfs(h.e[i].v,x);
--tp;
}
int main()
{
n=read(),m=read(),a=read(),b=read(),c=read();
for(int i=1,u,v;i<=m;i++)
g.add_edge(read(),read());
tarjan(a);
dfs(a,0);
for(int i=h.hd[b];i;i=h.e[i].nxt)
if(vs[h.e[i].v])
return puts("Yes"),0;
puts("No");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2022-09-04 [ABC261A] Intersection
2022-09-04 [ABC261B] Tournament Result
2022-09-04 [ABC261C] NewFolder(1)
2022-09-04 [ABC261F] Sorting Color Balls
2022-09-04 [ABC261D] Flipping and Bonus
2022-09-04 [ABC261E] Many Operations
2022-09-04 [ABC261Ex] Game on Graph