标签:i++ names turn closed getchar style color #define alt
【题解】
其实是个二分图最大匹配的模板题,直接上匈牙利算法就好了。
1 #include<cstdio> 2 #include<algorithm> 3 #define N 1010 4 #define rg register 5 using namespace std; 6 int n,m,E,ans,T,tot,last[N],v[N],from[N]; 7 struct edge{ 8 int to,pre; 9 }e[N*N]; 10 inline int read(){ 11 int k=0,f=1; char c=getchar(); 12 while(c<‘0‘||c>‘9‘)c==‘-‘&&(f=-1),c=getchar(); 13 while(‘0‘<=c&&c<=‘9‘)k=k*10+c-‘0‘,c=getchar(); 14 return k*f; 15 } 16 int dfs(int x){ 17 for(rg int i=last[x],to;i;i=e[i].pre) if(v[to=e[i].to]!=T){ 18 v[to]=T; 19 if(!from[to]||dfs(from[to])){ 20 from[to]=x; 21 return 1; 22 } 23 } 24 return 0; 25 } 26 int main(){ 27 n=read(); m=read(); 28 for(rg int i=1;i<=n;i++){ 29 int num=read(); 30 for(rg int j=1;j<=num;j++){ 31 int v=read(); 32 e[++tot]=(edge){v,last[i]};last[i]=tot; 33 } 34 } 35 for(rg int i=1;i<=n;i++) ++T,ans+=dfs(i); 36 printf("%d\n",ans); 37 return 0; 38 }
洛谷 1894 [USACO4.2]完美的牛栏The Perfect Stall
标签:i++ names turn closed getchar style color #define alt
原文地址:https://www.cnblogs.com/DriverLao/p/8909221.html