[toj1047]【二分图最大独立集】Girls and Boys

1047.   Girls and Boys
Time Limit: 5.0 Seconds    Memory Limit: 65536K
Total Runs: 1173    Accepted Runs: 385



the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition: there are no two students in the set who have been "romantically involved". The result of the program is the number of students in such a set.

The input contains several data sets in text format. Each data set represents one set of subjects of the study, with the following description:

the number of students
the description of each student, in the following format
student_identifier:(number_of_romantic_relations) student_identifier1 student_identifier2 student_identifier3 ...
or
student_identifier:(0)

The student_identifier is an integer number between 0 and n-1, for n subjects.
For each given data set, the program should write to standard output a line containing the result.


Sample Input

7
0: (3) 4 5 6
1: (2) 4 6
2: (0)
3: (0)
4: (2) 0 1
5: (1) 0
6: (2) 0 1
3
0: (2) 1 2
1: (1) 0
2: (1) 0


Sample Output

5
2



Source: Southeastern European 2000
题意:有N个熊孩子,之间有一些暧昧或者(基情)的关系,需要选出一拨人,他们间两两之间都没有暧昧(或基情),问这个集合最大是多少。题目没有说明男女的编号分别是什么,所以是一种混乱的关系,我们只需要认为同性之间也可以有这种关系即可。这样建图后我们发现所求是一个集合,集合中任意两点之间没有边,显然,这就是求最大独立集。我们有定理:|最大独立集|+|最小顶点覆盖|=|V|。一般图的最大独立集求解是NP困难的,但是对于二分图,我们有:|最小顶点覆盖|=|最大匹配数|。从而我们可以求出最大匹配数tot,然后n-tot即为最大独立集的大小。这里有一个问题需要注意,本题中关系是双向的,所以事实上对于每个人我们需要拆点,看成两个人,左边是0~n-1,右边也是0~n-1,然后就是求左边这波人和右边人间的最大匹配了,但是假设有匹配u-v,那么一定会有v-u,因为我们拆点了,所以这样求得的匹配数需要除以二,每个匹配都被重复算了一次。
代码

Show Code - Run ID 1488836
Submit Time: 2014-12-15 18:35:16     Language: GNU C++     Result: Accepted
    Pid: 1047     Time: 0.11 sec.     Memory: 956 K.     Code Length: 1.2 K.

/*
二分图:最大独立集=|V|-最小顶点覆盖=|V|-最大匹配
*/ 
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 512;
int match[MAX];
bool vis[MAX];
vector<int> G[MAX];
int n;

bool dfs(int u) {
	for (vector<int>::iterator it =  G[u].begin(); it != G[u].end(); ++it) {
		if (!vis[*it]) {
			vis[*it] = true;
			if (match[*it] == -1 || dfs(match[*it])) {
				match[*it] = u;
				return true;
			}
		}
	}
	return false;
}

inline int read() {
	char ch;
	while ((ch = getchar()) < '0' || ch > '9');
	int x = ch - '0';
	while ((ch = getchar()) >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
	}
	return x;
}

int main() {
	while (~scanf(" %d", &n)) {
		for (int i = 0; i < n; ++i) G[i].clear();
		
		int id, m, x;
		for (int i = 0; i < n; ++i) {
			id = read();
			m = read();
			while (m--) {
				x = read();
				G[id].push_back(x);
			}
		}
		
		memset(match, -1, sizeof(match));
		int tot = 0;
		for (int i = 0; i < n; ++i) {
			memset(vis, false, sizeof(vis));
			if (dfs(i)) ++tot;
		}
		printf("%d\n", n - (tot >> 1));
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值