1. 程式人生 > >3081 Marriage Match II 最大流 之 二分圖匹配 + 二分

3081 Marriage Match II 最大流 之 二分圖匹配 + 二分

                                          Marriage Match II

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

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

最大流 + 二分

二分最大匹配數

並查集處理朋友關係

設最大匹配數為k

建圖時,s = 0,t = 2*n+1

s 向所有 女孩(1-n) 連一條容量為k的邊

所有男孩向 t (n+1- 2*n) 連一條容量為k的邊

女孩向能與之配對的男孩連一條容量為1的邊

然後跑最大流 判斷是否 滿流 即 f = n*k

如果滿流則代表可配對方式至少有k種,然後二分------

p.s 這道題也可以用二分圖匹配做,以後補

然後雖然這樣建圖我勉強能接受了,但是還是有點不理解,因為能有k種一定能滿流n*k,

但是滿流 n*k 一定能推出有k種匹配方式嗎? 想不太清楚

唉 ~ 神奇的網路流建圖!

#include <bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 210;
const int maxm = 1e6+10;
#define pb(x)  push_back(x)
#define sc(x)  scanf("%d",&x)
#define pfn(x)  printf("%d\n",x)
#define pn    printf("\n");

int T,n,m,fn;

int h[maxn];
int l[maxn];
int cur[maxn];
int tot;

struct edge{
  int to,c,next;
  edge(int to = 0, int c = 0, int next = 0) : to(to), c(c), next(next) {}
}es[maxm*2];

void add_edge(int u, int v, int c)
{
  es[tot] = edge(v,c,h[u]);
  h[u] = tot++;
}

bool bfs(int s, int t)
{
  memset(l,0,sizeof(l));
  l[s] = 1;
  queue <int> q;
  q.push(s);
  while(!q.empty())
  {
    int u = q.front();
    q.pop();
    //cout << u << endl;
    if(u == t) return true;
    for(int i = h[u]; i != -1; i = es[i].next)
    {
      int v = es[i].to;
      if(!l[v] && es[i].c) {l[v] = l[u] + 1; q.push(v);}
    }
  }
  return false;
}

int dfs(int x, int t, int mf)
{
    if(x == t) return mf;
    int ret = 0;
    for(int &i = cur[x]; i != -1; i = es[i].next)
    {
      if(es[i].c && l[x] == l[es[i].to] - 1)
      {
        int f = dfs(es[i].to,t,min(mf,es[i].c));
        es[i].c -= f;
        es[i^1].c += f;
        ret += f;
        if(ret == mf) return ret;
      }
    }
    return ret;
}


int dinic(int s, int t)
{
  int res = 0;
  while(bfs(s,t))
  {
    for(int i = 0; i <= 2*n+1; i++) cur[i] = h[i];
    res += dfs(s,t,INF);
  //  //cout << "**************"<< res << endl;
  }
  return res;
}


bool g[maxn][maxn];
int f[maxn];
set <int> cnt[maxn];

void init()
{
  for(int i = 0; i <= n; i++) f[i] = i;
}

int find(int x)
{
  return f[x] == x ? x : f[x] = find(f[x]);
}

void unit(int x, int y)
{
  int fx = find(x);
  int fy = find(y);
  f[fx] = fy;
}

bool same(int x, int y)
{
  return find(x) == find(y);
}

bool check(int s, int t, int k)
{
  memset(h,-1,sizeof(h));
  tot = 0;
  for(int i = 1; i <= n; i++)
   {
     add_edge(s,i,k);add_edge(i,s,0);
     add_edge(n+i,t,k);add_edge(t,n+i,0);
   }
  for(int i = 1 ; i <= n; i++)
    {
      int fi = find(i);
      for(int j = 1; j <= n; j++)
      {
        if(cnt[fi].count(j) == 1)
        {
          add_edge(i,n+j,1);
          add_edge(n+j,i,0);
        }
      }
    }
  int res = dinic(s,t);
  //cout <<"*" <<res << endl;
  return res == n*k;
}


int main()
{
  sc(T);
  while(T--)
  {
    sc(n);sc(m);sc(fn);
    init();
    memset(g,false,sizeof(g));
    for(int i = 0; i <= n; i++) cnt[i].clear();
    for(int i = 1; i <= m; i++)
    {
      int a,b;
      sc(a);sc(b);
      g[a][b] = true;
    }

    for(int i = 0; i < fn; i++)
    {
      int a,b;
      sc(a);sc(b);
      unit(a,b);
    }
    for(int i = 1; i <= n; i++)
    {
      int fi = find(i);
      for(int j = 1; j <= n; j++)
      {
        if(g[i][j]) cnt[fi].insert(j);
      }
    }
    //for(int i = 1; i <= n; i++) //cout << cnt[i].size() << "&&&&"<<endl;
    int s = 0,t = 2*n+1;
    int st = 0,ed = n+1;
    while(ed - st > 1)
    {
      int mid = st + (ed - st)/2;
      //cout << st << " " << mid << " " << ed << endl;
      if(check(s,t,mid))  st = mid;
      else ed = mid;
    }
    //cout << "ans = ";
    pfn(st);
  }
  return 0;
}