• Source
    1. #include <iostream>
    2. #include <vector>
    3. #include <queue>
    4. using namespace std;
    5. const int N=109;
    6. vector<int> graph[N];
    7. bool mark[N];
    8. queue< pair<int,int> > q;
    9. int main()
    10. {
    11. int n;
    12. cin>>n;
    13. vector<int> ans[3];
    14. for(int i=1;i<=n;++i)
    15. {
    16. int b;
    17. cin>>b;
    18. while(b!=0)
    19. {
    20. graph[i].push_back(b);
    21. cin>>b;
    22. }
    23. }
    24. for(int i=1;i<=n;++i)
    25. {
    26. if(mark[i]==false)
    27. {
    28. q.push(make_pair(i,1));
    29. ans[1].push_back(i);
    30. mark[i]=true;
    31. while(!q.empty())
    32. {
    33. pair<int,int> p=q.front();
    34. q.pop();
    35. int t;
    36. if(p.second==1)
    37. t=2;
    38. else
    39. t=1;
    40. for(int i=0;i<graph[p.first].size();++i)
    41. if(mark[graph[p.first][i]]==false)
    42. {
    43. mark[graph[p.first][i]]=true;
    44. q.push(make_pair(graph[p.first][i],t));
    45. ans[t].push_back(graph[p.first][i]);
    46. }
    47. }
    48. }
    49. }
    50. bool team1[N],team2[N];
    51. for(int i=0;i<ans[1].size();++i)
    52. team1[ans[1][i]]=true;
    53. for(int i=0;i<ans[2].size();++i)
    54. team2[ans[2][i]]=true;
    55. bool check=true;
    56. for(int i=0;i<ans[1].size();++i)
    57. {
    58. check=false;
    59. for(int j=0;j<graph[ans[1][i]].size();++j)
    60. if(team2[graph[ans[1][i]][j]]==true)
    61. check=true;
    62. if(!check)
    63. break;
    64. }
    65. if(!check)
    66. cout<<0;
    67. else
    68. {
    69. cout<<ans[1].size()<<endl;
    70. for(int i=0;i<ans[1].size();++i)
    71. cout<<ans[1][i]<<" ";
    72. }
    73. return 0;
    74.  
    75. }