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?
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?
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.
1 4 5 2 1 1 2 3 3 2 4 2 4 4 1 4 2 3
2
思路见来自某大牛:
题目大意:n个男孩n个女孩,女孩选男孩,每个女孩都要选到不同的人k对女孩有相同选择标准,女孩每轮都选择没选过的男孩,问总共能选几轮。
题解:女孩1..n,男孩n+1..2*n编号由女孩到男孩建容量为1的边起点st=2*n+1,到1..n建边;n+1..2*n到终点ed=2*n+2建边二分搜索最大容量即为答案;#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 2e4+10; int n,m,f; struct Info{ int from,to,c,flow,next; }edge[maxn<<1]; int head[maxn],level[maxn],cnt; int fa[maxn]; void ADD(int u,int v,int w){ edge[cnt].from = u; edge[cnt].to = v; edge[cnt].c = edge[cnt].flow = w; edge[cnt].next = head[u]; head[u] = cnt++; edge[cnt].from = v; edge[cnt].to =u; edge[cnt].c = edge[cnt].flow = 0; edge[cnt].next = head[v]; head[v] = cnt++; } struct People{ int u,v; }pen[maxn]; int Find(int x){ if(x != fa[x]) fa[x] = Find(fa[x]); return fa[x]; } void Merge(int u,int v){ int fu = Find(u); int fv = Find(v); if(fu != fv) fa[fv] = fu; } int Map[120][120]; void build(int mid,int S,int E){ memset(head,-1,sizeof(head)); memset(Map,0,sizeof(Map)); cnt = 0; for(int i = 1; i <= n; i++){ ADD(S,i,mid); ADD(i+n,E,mid); } for(int i = 1; i <= m; i++){ int u = pen[i].u; int v = pen[i].v; for(int j = 1; j <= n; j++){ if(Find(j) == Find(u) && !Map[j][v]){ ADD(j,v+n,1); Map[j][v] = 1; } } } } bool bfs(int S,int E){ memset(level,-1,sizeof(level)); level[S] = 0; queue<int>Q; Q.push(S); while(!Q.empty()){ int u = Q.front(); Q.pop(); for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(level[v] == -1 && edge[i].flow>0){ level[v] = level[u]+1; Q.push(v); if(v == E) return 1; } } } return 0; } int dfs(int now,int flow,int E){ if(now == E)return flow; int os = flow; for(int i = head[now]; ~i; i = edge[i].next){ int v = edge[i].to; if(level[v] == level[now]+1 && edge[i].flow > 0){ int temp = dfs(v,min(flow,edge[i].flow),E); if(!temp){ level[v] = -1; continue; } edge[i].flow -= temp; edge[i^1].flow += temp; flow -= temp; if(!flow)break; } } return os-flow; } int Dinic(int S,int E){ int Maxflow = 0; while(bfs(S,E)){ int temp = dfs(S,INF,E); Maxflow += temp; } return Maxflow; } int main(){ int T,u,v; scanf("%d",&T); while(T--){ scanf("%d %d %d",&n,&m,&f); int S = 0,E = n*2+1; for(int i = 1; i <= n; i++) fa[i] = i; for(int i = 1; i <= m; i++) scanf("%d %d",&pen[i].u,&pen[i].v); for(int i = 1; i <= f; i++){ scanf("%d %d",&u,&v); Merge(u,v); } int l = 0,r = n; int ans = 0; while(l <= r){ int Mid = (l+r)>>1; build(Mid,S,E); if(Dinic(S,E) >= n*Mid){ l = Mid+1; ans = Mid; } else r = Mid-1; } printf("%d\n",ans); } return 0; }