题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068
思路:最大独立集 == |P| 减 【最大匹配(双向建图)】/2
还是用的vector建邻接表。。。速度比较快。。。

1 #include<iostream> 2 #include<vector> 3 const int N=507; 4 using namespace std; 5 int lx[N],ly[N]; 6 bool mark[N]; 7 vector<int>map[N]; 8 int n; 9 10 int dfs(int u){ 11 for(int i=0;i<map[u].size();i++){ 12 int k=map[u][i]; 13 if(!mark[k]){ 14 mark[k]=true; 15 if(ly[k]==-1||dfs(ly[k])){ 16 ly[k]=u; 17 lx[u]=k; 18 return true; 19 } 20 } 21 } 22 return false; 23 } 24 25 26 int MaxMatch(){ 27 int res=0; 28 memset(lx,-1,sizeof(lx)); 29 memset(ly,-1,sizeof(ly)); 30 for(int i=0;i<n;i++){ 31 if(lx[i]==-1){ 32 memset(mark,false,sizeof(mark)); 33 res+=dfs(i); 34 } 35 } 36 return res; 37 } 38 39 int main(){ 40 while(~scanf("%d",&n)){ 41 for(int i=0;i<n;i++)map[i].clear(); 42 int x,y,count; 43 for(int i=0;i<n;i++){ 44 scanf("%d: (%d)",&x,&count); 45 for(int i=1;i<=count;i++){ 46 scanf("%d",&y); 47 map[x].push_back(y); 48 map[y].push_back(x); 49 } 50 } 51 int ans=MaxMatch(); 52 printf("%d\n",n-ans/2); 53 } 54 return 0; 55 }