Question
哪儿都有
Solution
匈牙利裸题,但我作死 打了网络流。
Code
#include<cstdio>
#include<algorithm>
#define N 10010
using namespace std;
struct node{int v,fr,c;}e[N<<3];
int n,m,x,y,tail[N<<1],S=0,T,cnt=1,ans=0;
int dis[N<<1],gap[N<<1];
inline int read()
{
int x=0,f=0; char c=getchar();
while (c<'0' || c>'9') f=(c=='-') ? 1:f,c=getchar();
while (c>='0' && c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
return f ? -x:x;
}
inline void add(int u,int v)
{
e[++cnt]=(node){v,tail[u],1}; tail[u]=cnt;
e[++cnt]=(node){u,tail[v],0}; tail[v]=cnt;
}
int dfs(int x,int hav)
{
if (x==T) return hav;
int gone=0;
for (int p=tail[x],v,can;p;p=e[p].fr)
{
v=e[p].v;
if (dis[x]!=dis[v]+1) continue;
can=dfs(v,min(e[p].c,hav-gone));
e[p].c-=can;e[p^1].c+=can;gone+=can;
if (gone==hav) return gone;
}
gap[dis[x]]--;
if (!gap[dis[x]]) dis[1]=T;
gap[++dis[x]]++;
return gone;
}
int main()
{
freopen("air.in","r",stdin);
freopen("air.out","w",stdout);
n=read(),m=read();T=m+1;
x=read(),y=read();
while (x!=-1) add(x,y),x=read(),y=read();
for (int i=1;i<=n;i++) add(S,i);
for (int i=n+1;i<=m;i++) add(i,T);
while (dis[1]!=T) ans+=dfs(S,1000000);
if (ans==0) {puts("No Solution!"); return 0;}
printf("%d
",ans);
for (int i=1;i<=n;i++)
for (int p=tail[i];p;p=e[p].fr)
if (e[p].v>=n+1 && e[p].v<=m && e[p].c==0)
{printf("%d %d
",i,e[p].v); break;}
return 0;
}