HDU 3081 Marriage Match II

Marriage Match II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 457    Accepted Submission(s): 129

Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the game of playing house we used to play when we are kids. What a happy time as so many friends playing together. And it is normal that a fight or a quarrel breaks out, but we will still play together after that, because we are kids.  
Now, there are 2n kids, n boys numbered from 1 to n, and n girls numbered from 1 to n. you know, ladies first. So, every girl can choose a boy first, with whom she has not quarreled, to make up a family. Besides, the girl X can also choose boy Z to be her boyfriend when her friend, girl Y has not quarreled with him. Furthermore, the friendship is mutual, which means a and c are friends provided that a and b are friends and b and c are friend.  
Once every girl finds their boyfriends they will start a new round of this game—marriage match. At the end of each round, every girl will start to find a new boyfriend, who she has not chosen before. So the game goes on and on.
Now, here is the question for you, how many rounds can these 2n kids totally play this game?
 

 

Input
There are several test cases. First is a integer T, means the number of test cases.  
Each test case starts with three integer n, m and f in a line (3<=n<=100,0<m<n*n,0<=f<n). n means there are 2*n children, n girls(number from 1 to n) and n boys(number from 1 to n).
Then m lines follow. Each line contains two numbers a and b, means girl a and boy b had never quarreled with each other.  
Then f lines follow. Each line contains two numbers c and d, means girl c and girl d are good friends.
 

 

Output
For each case, output a number in one line. The maximal number of Marriage Match the children can play.
 

 

Sample Input
   
   
   
   
1 4 5 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
 

 

Sample Output
   
   
   
   
2
 

 

Author
starvae
 

 

Source
HDU 2nd “Vegetable-Birds Cup” Programming Open Contest
 

 

Recommend
lcy
 

 

Statistic |  Submit |  Discuss  | Back

 

 

 

我错误的代码啊,至今没发现哪里错了。。。。 用FLOYED写得,谁跟我说一下 #include<cstdio> #include<cstring> const int N=510; const int M=300001; const int inf=0x7fffffff; int head[N]; int map[310][310]; struct Edge { int v,next,w; } edge[M]; int cnt,n,s,t,m,f; void addedge(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++; } int sap() { int pre[N],cur[N],dis[N],gap[N]; int flow=0,aug=inf,u; bool flag; for(int i=0; i<n; i++) { cur[i]=head[i]; gap[i]=dis[i]=0; } gap[s]=n; u=pre[s]=s; while(dis[s]<n) { flag=0; for(int &j=cur[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[u]==dis[v]+1) { flag=1; if(edge[j].w<aug) aug=edge[j].w; pre[v]=u; u=v; if(u==t) { flow+=aug; while(u!=s) { u=pre[u]; edge[cur[u]].w-=aug; edge[cur[u]^1].w+=aug; } aug=inf; } break; } } if(flag) continue; int mindis=n; for(int j=head[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[v]<mindis) { mindis=dis[v]; cur[u]=j; } } if((--gap[dis[u]])==0) break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow; } void floyed() { for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=n+1; j<=n+n; j++) if(map[i][k]&&map[k][j]) map[i][j]=true; } bool iSAP(int lim) { s=0; t=n*2+1; cnt=0; memset(head,-1,sizeof(head)); for(int i=1; i<=n; i++) addedge(s,i,lim),addedge(i+n,t,lim); for(int i=1;i<=n;i++) for(int j=n+1;j<=n+n;j++) if(map[i][j]) addedge(i,j,1); int nn=n; n=n*2+2; int t=sap(); n=nn; if(t==n*lim) return true; return false; } void solve() { int head=0,end=n,ans=0; while(head<=end) { int mid=(head+end)>>1; if(iSAP(mid)) { ans=mid; head=mid+1; } else end=mid-1; } printf("%d/n",ans); } int main() { int T; scanf("%d",&T); while(T--) { memset(map,false,sizeof(map)); scanf("%d%d%d",&n,&m,&f); for(int i=1; i<=m; i++) { int x,y; scanf("%d%d",&x,&y); map[x][y+n]=true; } for(int i=1; i<=f; i++) { int x,y; scanf("%d%d",&x,&y); map[x][y]=map[y][x]=true; } floyed(); solve(); } return 0; }

 

 

跟着网上的人用并查集能过,还有这题目很恶心,TLE,前面。。。。 #include<cstdio> #include<cstring> const int N=510; const int M=300001; const int inf=0x7fffffff; int head[N]; int map[310][310]; int p[310]; struct Edge { int v,next,w; } edge[M]; struct E { int x,y; }e[300000]; int cnt,n,s,t,m,f; void addedge(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].w=0; edge[cnt].next=head[v]; head[v]=cnt++; } int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } void Union(int a,int b) { a=find(a); b=find(b); p[b]=a; } int sap() { int pre[N],cur[N],dis[N],gap[N]; int flow=0,aug=inf,u; bool flag; for(int i=0; i<n; i++) { cur[i]=head[i]; gap[i]=dis[i]=0; } gap[s]=n; u=pre[s]=s; while(dis[s]<n) { flag=0; for(int &j=cur[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[u]==dis[v]+1) { flag=1; if(edge[j].w<aug) aug=edge[j].w; pre[v]=u; u=v; if(u==t) { flow+=aug; while(u!=s) { u=pre[u]; edge[cur[u]].w-=aug; edge[cur[u]^1].w+=aug; } aug=inf; } break; } } if(flag) continue; int mindis=n; for(int j=head[u]; j!=-1; j=edge[j].next) { int v=edge[j].v; if(edge[j].w>0&&dis[v]<mindis) { mindis=dis[v]; cur[u]=j; } } if((--gap[dis[u]])==0) break; gap[dis[u]=mindis+1]++; u=pre[u]; } return flow; } bool iSAP(int lim) { s=0; t=n*2+1; cnt=0; memset(head,-1,sizeof(head)); memset(map,0,sizeof(map)); for(int i=1; i<=n; i++) addedge(s,i,lim),addedge(i+n,t,lim); /*for(int i=1;i<=n;i++) for(int j=n+1;j<=n+n;j++) if(map[i][j]) { //int a=find(i); //int b=find(j); // if(a==b) addedge(i,j,1); } */ for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(p[j]==p[e[i].x]&&!map[j][e[i].y+n]) { map[j][e[i].y+n]=1; addedge(j,e[i].y+n,1); } } } int nn=n; n=n*2+2; int t=sap(); n=nn; if(t==n*lim) return true; return false; } void solve() { for(int i=1;i<=n;i++) find(i); int head=0,end=n,ans=0; while(head<=end) { int mid=(head+end)>>1; if(iSAP(mid)) { ans=mid; head=mid+1; } else end=mid-1; } printf("%d/n",ans); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&f); for(int i=1;i<=2*n;i++) p[i]=i; for(int i=1; i<=m; i++) scanf("%d%d",&e[i].x,&e[i].y); for(int i=1; i<=f; i++) { int x,y; scanf("%d%d",&x,&y); Union(x,y); } solve(); } return 0; }

 

 

   

 

 

你可能感兴趣的:(struct,Integer,SAP,each,output,Numbers)