Description
平面上有一组点,它们之间有若干条线段相连。现在请你找到一个集合,使得在这个集合中没有任意两个点被线段相连。你只需要给出最大集合的元素即可。
Input
多行输入,第一行一个数N,表示有N个点,之后的N行每行以这样的形式给出:
点的标号: (与这个点相连的点的个数) 点的标号 点的标号……
或者
点的标号: (0)
当输入的N为0时退出程序。详见Sample。
Output
最大集合的元素个数。
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
0
Sample Output
5
2
Reference code
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N=503;
int e[N][N],match[N],n;
int book[N];
int dfs(int u){
for(int i=0;i<n;i++){
if(book[i]==0 && e[u][i]==1){
book[i]=1;
if(match[i]==-1 || dfs(match[i])){
match[i]=u;
return 1;
}
}
}
return 0;
}
int main(){
while(~scanf("%d",&n)){
memset(e,0,sizeof e);
memset(match,-1,sizeof match);
for(int i=0;i<n;i++){
int k,cnt;
scanf("%d: (%d)",&k,&cnt);
for(int j=0;j<cnt;j++){
int x;
scanf("%d",&x);
e[i][x]=1;
}
}
int ans=0;
for(int i=0;i<n;i++){
memset(book,0,sizeof(book));
if(dfs(i))
ans++;
}
printf("%d\n",n-ans/2);
}
return 0;
}