Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<map>
  4. #include<string>
  5. #include<list>
  6. #include<stack>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. int x,t;
  13. scanf("%d",&t);
  14.  
  15. for (x=1; x<=t; ++x)
  16. {
  17. map <string,int> id;
  18. map <int,string> word;
  19. list <int> synlist[2001];
  20. list <int> antlist[2001];
  21.  
  22. int n, i, j, m, num= 0,synarr[2001],antarr[2001],k= 0,l= 0;
  23. bool synstore[2001][2001]= {0}, antstore[2001][2001]= {0};
  24. char s[1000],s1[30],s2[30];
  25.  
  26. scanf("%d%d",&n,&m);
  27. getchar();
  28.  
  29. while (n--)
  30. {
  31. scanf("%[^\n]",s);
  32. //puts(s);
  33. sscanf(s,"%s %s",s1,s2);
  34. //puts(s1);
  35. //puts(s2);
  36. if (!id[s1]) id[s1]= ++num;
  37. synarr[++k]= num;
  38. if (!id[s2]) id[s2]= ++num;
  39. synarr[++k]= num;
  40. synlist[ id[s1] ].push_back(id[s2]);
  41. synlist[ id[s2] ].push_back(id[s1]);
  42. synstore[ id[s1] ][ id[s2] ]= 1;
  43. synstore[ id[s2] ][ id[s1] ]= 1;
  44.  
  45. word[ id[s1] ]= s1;
  46. word[ id[s2] ]= s2;
  47. getchar();
  48. }
  49. while (m--)
  50. {
  51. scanf("%[^\n]",s);
  52. sscanf(s,"%s %s",s1,s2);
  53. //puts(s);
  54. //puts(s1);
  55. //puts(s2);
  56. if (!id[s1]) id[s1]= ++num;
  57. antarr[++l]= num;
  58. if (!id[s2]) id[s2]= ++num;
  59. antarr[++l]= num;
  60. antlist[ id[s1] ].push_back(id[s2]);
  61. antlist[ id[s2] ].push_back(id[s1]);
  62. antstore[ id[s1] ][ id[s2] ]= 1;
  63. antstore[ id[s2] ][ id[s1] ]= 1;
  64.  
  65. word[ id[s1] ]= s1;
  66. word[ id[s2] ]= s2;
  67. getchar();
  68. }
  69. int start,end;
  70. bool flag= 0;
  71.  
  72. for (i=1; i<=l; ++i)
  73. {
  74. start= antarr[i];
  75.  
  76. list <int> ::iterator it;
  77. list <int> ::iterator it2;
  78.  
  79. for (it= antlist[start].begin(); it!= antlist[start].end(); ++it)
  80. {
  81. for (it2= antlist[*it].begin(); it2!= antlist[*it].end(); ++it2)
  82. {
  83. synlist[start].push_back(*it2);
  84. synlist[*it2].push_back(start);
  85. synstore[start][*it2]= 1;
  86. synstore[*it2][start]= 1;
  87.  
  88. if (antstore[start][*it2])
  89. {
  90. flag= 1;
  91. break;
  92. }
  93. }
  94. if (flag) break;
  95. }
  96. if (flag) break;
  97. }
  98.  
  99. if (!flag)
  100. {
  101. bool index[4001]= {0};
  102. int nodes= 0, set[4001], p= 0;
  103.  
  104. for (i=1; i<=k; ++i)
  105. {
  106. start= synarr[i];
  107.  
  108. if (index[start]) continue;
  109. p= 0;
  110. index[start]= 1;
  111. set[++p]= start;
  112. nodes++;
  113.  
  114. stack <int> act;
  115. stack <int> wait;
  116. wait.push(start);
  117.  
  118. do
  119. {
  120. while (!wait.empty())
  121. {
  122. act.push( wait.top() );
  123. wait.pop();
  124. }
  125. while ( !act.empty() )
  126. {
  127. start= act.top();
  128. act.pop();
  129.  
  130. list <int> ::iterator it;
  131. for (it= synlist[start].begin(); it!= synlist[start].end(); ++it)
  132. {
  133. if (index[*it]) continue;
  134. index[*it]= 1;
  135. set[++p]= *it;
  136. wait.push(*it);
  137. nodes++;
  138. if (nodes==k) break;
  139. }
  140. if (nodes==k) break;
  141. }
  142. if (nodes==k) break;
  143. }
  144. while (!wait.empty());
  145.  
  146. //for (j=1; j<=p; ++j) cout << word[ set[j] ] << "<<" ;
  147. //puts("");
  148.  
  149. for (j=1; j<=p; ++j)
  150. {
  151. for (l=1; l<=p; ++l)
  152. {
  153. if (j==l) continue;
  154.  
  155. synstore[ set[j] ][ set[l] ]= 1;
  156. synstore[ set[l] ][ set[j] ]= 1;
  157. //cout << word[set[j]] << " " << word[set[l]] << endl;
  158.  
  159. if (antstore[ set[j] ][ set[l] ])
  160. {
  161. //cout << word[set[j]] << " " << word[set[l]] << endl;
  162. flag= 1;
  163. break;
  164. }
  165. }
  166. if (flag) break;
  167. }
  168. if (flag || nodes==k) break;
  169. }
  170. }
  171.  
  172.  
  173.  
  174. // outputs
  175. printf("Case %d: ",x);
  176. if (flag) puts("NO");
  177. else puts("YES");
  178. }
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement