poj 1466 Girls and Boys

题意:给定n个学生和每个学生与其他人之间的关系,关系只能在男女之间存在,求最大的集合:集合内任意两个学生都没有关系,即最大独立集。


分析:独立数+点覆盖数 = N,所以求出匹配数即可。

Ps:对于双向边刚开始我只保存了一条边,结果WA了,因为没有分成男女两个集合,所以求出来的有问题;如果保存双向边,求出最大匹配除以2即可。


附上一组数据:

4
0: (2) 1 3
1: (2) 0 2
2: (2) 1 3
3: (2) 0 2

正确答案是2,而错误的代码得到的答案是1。


以下附上代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int maxn = 505;

int n;
int g[maxn][maxn];
int cx[maxn],cy[maxn];
bool vis[maxn];

void input()
{
  int i,j;
  int u,v;
  int k;
  
  for(i = 0; i < n; i++){
    for(j = 0; j < n; j++){
      g[i][j] = 0;
    }
  }
  
  for(i = 0; i < n; i++){
    scanf("%d: (%d)",&u,&k);
    for(j = 0; j < k; j++){
      scanf("%d",&v);
    
      g[u][v] = 1;
    }
  }
  
}


int path(int u)
{
  for(int v = 0; v < n; v++){
    if(g[u][v] && !vis[v]){
      vis[v] = 1;
      if(cy[v] == -1 || path(cy[v])){
        cx[u] = v;
        cy[v] = u;
        return 1;
      }
    }
  }
  return 0;
}

int maxMatch()
{
  fill(cx,cx+n,-1);
  fill(cy,cy+n,-1);
  
  int res = 0;
  
  for(int i = 0; i < n; i++){
    if(cx[i] == -1){//未匹配 
      fill(vis,vis+n,0);
      res += path(i);
    }
  }
  
  return res/2;
}

void solve()
{
  printf("%d\n",n-maxMatch());
}

int main()
{
  while(scanf("%d",&n) != EOF){
    input();
    solve();
  }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值