标签:des style blog color java os io strong
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2458 Accepted Submission(s): 1177
1 #include<string> 2 #include<cstdio> 3 #include<iostream> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #include<cstring> 9 #include<stdlib.h> 10 #include<string> 11 #include<cmath> 12 using namespace std; 13 #define pb push_back 14 #define ll __int64 15 int fa[210],vit[210],p[210][210],x[210],y[210],cnt1,cnt2,cx[210]; 16 vector<int >ko[210]; 17 int n,m; 18 int Find(int pos){ 19 for(int i=0;i<ko[pos].size();i++){ 20 int to=ko[pos][i]; 21 if(!vit[to]){ 22 vit[to]=1; 23 if(!cx[to]||Find(cx[to])){ 24 cx[to]=pos; 25 return 1; 26 } 27 } 28 } 29 return 0; 30 } 31 void solve(){ 32 int cnt=0; 33 memset(cx,0,sizeof(cx)); 34 for(int i=1;i<=cnt1;i++){ 35 memset(vit,0,sizeof(vit)); 36 if(Find(x[i])) cnt++; 37 } 38 cout<<cnt<<endl; 39 } 40 int main(){ 41 while(cin>>n>>m){ 42 for(int i=1;i<=n;i++) ko[i].clear(); 43 memset(p,0,sizeof(p)); 44 memset(vit,0,sizeof(vit)); 45 for(int i=1;i<=m;i++){ 46 int a,b;scanf("%d%d",&a,&b); 47 p[a][b]=1,p[b][a]=1; 48 ko[a].pb(b); 49 ko[b].pb(a); 50 } 51 for(int i=1;i<=n;i++) 52 if(!vit[i]){ 53 vit[i]=1; 54 fa[i]=0; 55 queue<int >kk; 56 kk.push(i); 57 while(!kk.empty()){ 58 int pos=kk.front(); 59 kk.pop(); 60 for(int j=0;j<ko[pos].size();j++){ 61 int to=ko[pos][j]; 62 if(!vit[to]){ 63 fa[to]=!fa[pos]; //把相互认识的人放在不同的集合里里面 64 vit[to]=1; 65 kk.push(to); 66 } 67 } 68 } 69 } 70 cnt1=cnt2=0; 71 for(int i=1;i<=n;i++) 72 if(!fa[i]) x[++cnt1]=i; 73 for(int i=1;i<=n;i++) 74 if(fa[i]) y[++cnt2]=i; 75 int can=1; 76 for(int i=1;i<=cnt1;i++) 77 for(int j=1;j<=cnt1;j++) 78 if(p[x[i]][x[j]]) can=0; 79 for(int i=1;i<=cnt2;i++) 80 for(int j=1;j<=cnt2;j++) 81 if(p[y[i]][y[j]]) can=0; 82 if(!can){ 83 cout<<"No"<<endl; 84 continue; 85 } 86 else solve();//不存在那种情况,进行匹配 87 } 88 }
hdu 2444 The Accomodation of Students,布布扣,bubuko.com
hdu 2444 The Accomodation of Students
标签:des style blog color java os io strong
原文地址:http://www.cnblogs.com/ainixu1314/p/3901779.html