HDU 3277 Marriage Match III 最大流 + 二分 + 并查集 + 拆点建图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82825192

                                     Marriage Match III

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


 

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

 

1

4 5 1 2

1 1

2 3

3 2

4 2

4 4

1 4

2 3

Sample Output

3

Author

starvae

HDU 3081 Marriage Match II 一样, 只不过多了女生可以额外选k个吵架的男生配对,所以只要女生拆点 然后连一条容量为k的边到拆出的点,再有拆出的点连向吵架的男生,容量为1,其他建图如 HDU 3081 Marriage Match II 。

#include <bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 250*3+100;
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,kn,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-ret,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 <= 3*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);
     add_edge(i,2*n+i,kn);add_edge(2*n+i,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);
        }
        else
        {
          add_edge(2*n+i,n+j,1);
          add_edge(n+j,2*n+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(kn);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 = 3*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;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82825192
今日推荐