1
解题思路:二分答案。然后最大流判断。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m,len,S,T;
long long ans;
const int INF=0x7fffffff;
int to[100000],next[100000],f[100000];
int h[20010],dis[20010],q[1000000];
int x[10010],y[10010];
inline int read()
{
char y; int x=0,f=1; y=getchar();
while (y<'0'||y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0' && y<='9') {x=x*10+int(y)-48; y=getchar();}
return x*f;
}
void insert(int x,int y,int flow)
{
++len; to[len]=y; next[len]=h[x]; f[len]=flow; h[x]=len;
}
void buildmap(int mg)
{
len=1;
memset(h,0,sizeof(h));
for (int i=1;i<=n;++i)
{
insert(S,i,mg); insert(i,S,0);
}
for (int i=1;i<=m;++i)
{
insert(x[i],n+i,1); insert(i+n,x[i],0);
insert(y[i],n+i,1); insert(i+n,y[i],0);
insert(n+i,T,1); insert(T,n+i,0);
}
}
bool bfs()
{
memset(dis,-1,sizeof(dis)); dis[S]=0;
int tail,head=0; tail=1; q[tail]=S;
while (head<tail)
{
++head;
int u=h[q[head]];
while (u!=0)
{
if (f[u]>0&&dis[to[u]]==-1)
{
dis[to[u]]=dis[q[head]]+1;
++tail; q[tail]=to[u];
}
u=next[u];
}
}
if (dis[T]!=-1)return true;else return false;
}
int dinic(int now,int sum)
{
if(now==T) return sum;
int sug=0;
int u=h[now];
while (u!=0)
{
if (f[u]>0 && dis[to[u]]==dis[now]+1)
{
int s=dinic(to[u],min(sum-sug,f[u]));
if (s)
{
sug+=s;
f[u]-=s; f[u^1]+=s;
if (sug==sum)return sug;
}
}
u=next[u];
}
if (sug==0)dis[now]=-1;return sug;
}
int main()
{
n=read(); m=read(); S=0; T=20001;
for (int i=1;i<=m;++i)
{
x[i]=read(); y[i]=read();
}
int l=0; int r=10000;
while (l<r)
{
int mg=(l+r)/2;
buildmap(mg);
ans=0;
while (bfs())
ans+=dinic(S,INF);
if (ans>=m) r=mg;else l=mg+1;
}
printf("%d",l);
}