hdu 3277 Marriage Match III(二分最大流+并查集+判断满流+拆点)

来源:互联网 发布:linux find exec用法 编辑:程序博客网 时间:2024/06/06 05:53

Marriage Match III

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1011    Accepted Submission(s): 293


Problem Description
Presumably, you all have known the question of stable marriage match. A girl will choose a boy; it is similar as the ever game of play-house . What a happy time as so many friends play 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. As 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. On the other hand, in order to play more times of marriage match, every girl can accept any K boys. If a girl chooses a boy, the boy must accept her unconditionally whether they had quarreled before or not. 

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 an integer T, means the number of test cases. 
Each test case starts with three integer n, m, K and f in a line (3<=n<=250, 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
14 5 1 21 12 33 24 24 41 42 3
 

Sample Output
3
题意:和hdu3081差不多,3081的题意:有n个女生和n个男生,给出m个关系a b,表示a能与b配对。给出f个关系a b,表示a与b是朋友。若a与c能配对,而a与b是朋友,则b与c也能配对。游戏进行多轮,每一轮每个女生找之前都没有配对过的男生配对,问这个游戏最多能进行多少轮。
而这道题多了一个条件,就是女生可以选择任意k个男生配对,不管是否喜欢。
思路:和hdu3081一样,二分最大流,然后判断是否满流,但这题多了拆点,将女生i拆成两个点i和i+n,若该女生能与男生j配对,则连一条i到j的边,否则,连一条i+n到j的边,容量都为1。然后对于每个女生i,连一条i到i+n的边,容量为k,保证了能与k个男生配对。因为这道题二分最大流时只需改变源点到女生、男生到汇点的容量,所以我们可以开始时就建好图,每次二分时改变一些边的容量即可,这可以减小运行时间。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <cmath>#include <cstdlib>#define L(rt) (rt<<1)#define R(rt) (rt<<1|1)#define ll __int64#define eps 1e-6using namespace std;const int INF = 1000000007;const int maxn = 1000;struct Edge{    int u, v, cap, flow, next;}et[maxn*maxn],cet[maxn*maxn];struct node{    int girl,boy;}match[maxn*maxn];int low[maxn], cur[maxn], cnt[maxn], dis[maxn], pre[maxn], eh[maxn];int fa[maxn];bool G[maxn][maxn];int s, t, n, m, k, f, num;inline void init(){    memset(eh, -1, sizeof(eh));    num = 0;}inline void add(int u, int v, int cap, int flow){    Edge e = {u, v, cap, flow, eh[u]};    et[num] = e;    eh[u] = num++;}inline void addedge(int u, int v, int cap){    add(u, v, cap, 0);    add(v, u, 0, 0);}int find(int x){return x == fa[x] ? x : fa[x] = find(fa[x]); }inline void Union(int a, int b){fa[find(b)] = find(a); }void make_set(){for(int i = 1; i <= n; i++) fa[i] = i;}void build_graph(){    init();    memset(G, 0, sizeof(G));    for(int i = 1; i <= n; i++)    {        addedge(s, i, 0);        addedge(i + 2 * n, t, 0);    }    for(int i = 1; i <= n; i++) addedge(i, i + n, k);    for(int i = 0; i < m; i++)    {        int u = match[i].girl, v = match[i].boy;        for(int j = 1; j <= n; j++)        if(find(u) == find(j) && !G[j][v])        {            G[j][v] = 1;            addedge(j, v + 2 * n, 1);        }    }    for(int i = 1; i <= n; i++)    for(int j = 1; j <= n; j++)    if(!G[i][j]) addedge(i + n, j + 2 * n, 1);    for(int i = 0; i < num; i++) cet[i] = et[i];}void update_graph(int cap){    for(int i = 0; i < num; i++) et[i] = cet[i];    for(int i = 0; i < 4 * n; i += 2)    et[i].cap = cap;}int isap(int s, int t, int nv){    int u, v, now, flow = 0;    memset(cnt, 0, sizeof(cnt));    memset(low, 0, sizeof(low));    memset(dis, 0, sizeof(dis));    for(u = 0; u <=nv; u++) cur[u] = eh[u];    low[s] = INF, cnt[0] = nv, u = s;    while(dis[s] < nv)    {        for(now = cur[u]; now != -1; now = et[now].next)        if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;        if(now != -1)        {            cur[u] = pre[v] = now;            low[v] = min(et[now].cap - et[now].flow, low[u]);            u = v;            if(u == t)            {                for(; u != s; u = et[pre[u]].u)                {                    et[pre[u]].flow += low[t];                    et[pre[u]^1].flow -= low[t];                }                flow += low[t];                low[s] = INF;            }        }        else        {            if(--cnt[dis[u]] == 0) break;            dis[u] = nv, cur[u] = eh[u];            for(now = eh[u]; now!= -1; now = et[now].next)            if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)            dis[u] = dis[et[now].v] + 1;            cnt[dis[u]]++;            if(u != s) u = et[pre[u]].u;        }    }    return flow;}int main(){    int a, b, tt;    scanf("%d", &tt);    while(tt--)    {        scanf("%d%d%d%d", &n, &m, &k, &f);        s = 0;        t = 3 * n + 1;        make_set();        for(int i = 0; i < m; i++)        scanf("%d%d", &match[i].girl, &match[i].boy);        while(f--)        {            scanf("%d%d", &a, &b);            Union(a, b);        }        build_graph();        int low = 0, high = n+1, mid, ans;        while(low <= high)        {            mid = (low + high) >> 1;            update_graph(mid);            if(isap(s, t, t+1) == mid * n)            {                ans = mid;                low = mid + 1;            }            else high = mid - 1;        }        printf("%d\n", ans);    }    return 0;}