- 题意:学校对n个学生(男女都有)进行的调查了,发现了某些学生暗生情愫,现在需要你选出一个最大的集合,这个集合内部没有两个人暗生情愫。学生的编号是0~n-1The 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”.
- 思路:模板最大独立集。注意while输入~~~~scanf
-
#include<bits/stdc++.h> using namespace std; #define maxn 1001 int n,net[maxn],x,m; vector<int>mmp[maxn]; bool vis[maxn]; bool dfs(int u) { for(int i=0; i<mmp[u].size(); i++) { int v=mmp[u][i]; if(!vis[v]) { vis[v]=1; if(net[v]==-1||dfs(net[v])) { net[v]=u; net[u]=v; return true; } } } return false; } int main() { while(~scanf("%d",&n)) { for(int i=0; i<n; i++) { net[i]=-1; scanf("%d: (%d)",&x,&m); while(m--) { scanf("%d",&x); mmp[i].push_back(x); } } x=0; for(int i=0; i<n; i++) if(net[i]<0) { memset(vis,0,sizeof(vis)); if(dfs(i)) x++; } printf("%d\n",n-x); for(int i=0; i<n; i++) mmp[i].clear(); } return 0; }