fork(3) download
  1. #include<iostream>
  2. #include<vector>
  3. #include<set>
  4. #include<map>
  5. #include<string>
  6. using namespace std;
  7.  
  8. map<string,set<string>> G;
  9. map<string,bool> used;
  10. map<string,bool> rec;
  11. vector<string> V;
  12.  
  13. void dfs(string cur, string aim){
  14. if(used[cur] || rec[aim]) return;
  15. used[cur] = 1;
  16. for(auto i = G[cur].begin(); i != G[cur].end(); i++){
  17. if(*i != aim){
  18. dfs(*i,aim);
  19. }else{
  20. rec[aim] = 1;
  21. return;
  22. }
  23. }
  24. }
  25.  
  26. int main(){
  27. //input
  28. int n; cin >> n;
  29. for(int i = 0; i < n; i++){
  30. string curVert; cin >> curVert;
  31. V.push_back(curVert);
  32. int m; cin >> m;
  33. while(m--){
  34. string inp; cin >> inp;
  35. G[curVert].insert(inp);
  36. }
  37. if(i != n-1){
  38. string stars;
  39. cin >> stars;
  40. }
  41. }
  42. //work
  43. for(auto i : V){
  44. dfs(i, i);
  45. used.clear();
  46. }
  47. for(auto i : V){
  48. cout << i << (rec[i] ? ": YES" : ": NO") << endl;
  49. }
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 3288KB
stdin
3
p1
2
p1
p2
*****
p2
1 
p1
*****
p3
1
p1
stdout
p1: YES
p2: YES
p3: NO