标签:ups eof logs 二分图匹配 ems his key memory class
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7087 Accepted Submission(s): 3168
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 6 using namespace std; 7 8 const int N(233); 9 int n,m,head[N],sumedge; 10 struct Edge 11 { 12 int v,next; 13 Edge(int v=0,int next=0):v(v),next(next){} 14 }edge[233333]; 15 inline void ins(int u,int v) 16 { 17 edge[++sumedge]=Edge(v,head[u]); 18 head[u]=sumedge; 19 edge[++sumedge]=Edge(u,head[v]); 20 head[v]=sumedge; 21 } 22 23 int col[N]; 24 bool Paint(int x) 25 { 26 col[x]=0; 27 queue<int>que; 28 que.push(x); 29 for(int u,v;!que.empty();) 30 { 31 u=que.front(); que.pop(); 32 for(int i=head[u];i;i=edge[i].next) 33 { 34 v=edge[i].v; 35 if(col[v]!=-1) 36 { 37 if(col[v]==col[u]) 38 return false; 39 } 40 else 41 { 42 col[v]=col[u]^1; 43 que.push(v); 44 } 45 } 46 } 47 return true; 48 } 49 50 int sumvis,vis[N],match[N],Map[N][N]; 51 bool find(int u) 52 { 53 /*for(int v,i=head[i];i;i=edge[i].next) 54 { 55 v=edge[i].v; 56 if(!vis[v]) 57 { 58 vis[v]=1; 59 if(!match[v]||find(match[v])) 60 { 61 match[v]=u; 62 return true; 63 } 64 } 65 }*/ 66 for(int v=1;v<=n;v++) 67 if(Map[u][v]&&!vis[v]) 68 { 69 vis[v]=1; 70 if(!match[v]||find(match[v])) 71 { 72 match[v]=u; 73 return true; 74 } 75 } 76 return false; 77 } 78 79 inline void init() 80 { 81 memset(edge,0,sizeof(edge)); 82 memset(head,0,sizeof(head)); 83 memset(match,0,sizeof(match)); 84 } 85 86 int main() 87 { 88 for(;~scanf("%d%d",&n,&m);init()) 89 { 90 int ans=0,flag=0;sumvis=0; 91 memset(Map,0,sizeof(Map)); 92 for(int u,v;m--;ins(u,v)) 93 scanf("%d%d",&u,&v),Map[u][v]=1; 94 memset(col,-1,sizeof(col)); 95 for(int i=1;i<=n;i++) 96 if(col[i]==-1) 97 if(!Paint(i)) 98 { 99 flag=1; 100 break; 101 } 102 if(flag) 103 { 104 puts("No"); 105 continue; 106 } 107 for(int i=1;i<=n;i++) 108 { 109 if(find(i)) ans++; 110 memset(vis,0,sizeof(vis)); 111 } 112 printf("%d\n",ans); 113 } 114 return 0; 115 }
HDU——T 2444 The Accomodation of Students
标签:ups eof logs 二分图匹配 ems his key memory class
原文地址:http://www.cnblogs.com/Shy-key/p/7435403.html