poj3692 Kindergarten 二分图最大独立集

幼儿园中有n个男孩m个女孩,男孩之间都认识,女孩之间都认识,求一个极大团。

建出补图后就转化成了求该图独立集的问题,因为补图是二分图,于是匹配。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>

#define N 5005
#define M 500005
using namespace std;
int n , m , s , t;
struct arc
{
  int x , f , next;
}e[M];
int pre[N] , mcnt;

void addarc(int x ,int y ,int z)
{
  e[mcnt] = (arc) {y , z , pre[x]} , pre[x] = mcnt ++;
  e[mcnt] = (arc) {x , 0 , pre[y]} , pre[y] = mcnt ++;
}

int d[N] , cur[N];
bool f[N];

bool BFS()
{
  memset(f, 0 ,sizeof(f));
  queue<int> q;
  q.push(s) , d[s] = 0 , f[s] = 1;
  while (!q.empty())
  {
    int x = q.front() ; q.pop();
    for (int i = pre[x] ; ~i ;i = e[i].next)
    {
      arc& a = e[i];
      if (!f[a.x] && a.f)
        f[a.x] = 1 , d[a.x] = d[x] + 1 , q.push(a.x);
    }
  }
  return f[t];
}

int DFS(int x , int flow)
{
  if (x == t || !flow) return flow;
  int sum = 0 , u;
  for (int& i = cur[x] ; ~i ;i = e[i].next)
  {
    arc& a = e[i];
    if (d[a.x] == d[x] + 1 && (u = DFS(a.x , min(flow , a.f))) > 0 )
    {
      a.f -= u ,  e[i ^ 1].f += u;
      sum += u , flow -= u;
      if (!flow) break;
    }
  }
  return sum;
}

int dinic()
{
  int ans = 0;
  while (BFS())
  {
    memcpy(cur , pre , (t + 1) * sizeof(int));
    ans += DFS(s , 1 << 30);
  }
  return ans;
}

int k , ca;
bool g[305][305];
void work()
{
  int i , j , x , y;
  printf("Case %d: " , ++ ca);
  scanf("%d%d",&m,&k);
  memset(pre , -1 , sizeof(pre));
  mcnt = 0 , s = n + m + 1 , t = s + 1;
  memset(g , 1 , sizeof(g));
  while (k --)
    scanf("%d%d",&x,&y) , g[x][y] = 0;
  for (i = 1 ; i <= n ; ++ i) addarc(s , i , 1);
  for (i = 1 ; i <= m ; ++ i) addarc(i + n , t , 1);
  for (i = 1 ; i <= n ; ++ i)
    for (j = 1 ; j <= m ; ++ j)
      if (g[i][j])
        addarc(i , j + n , 1);
  cout << n + m - dinic() << endl;
}


int main()
{
  while(scanf("%d", &n) , n)
  //int _;cin>>_;while(_--)
    work();
  return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值