Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<iostream>
- #include<map>
- #include<string>
- #include<list>
- #include<stack>
- using namespace std;
- int main()
- {
- int x,t;
- scanf("%d",&t);
- for (x=1; x<=t; ++x)
- {
- map <string,int> id;
- map <int,string> word;
- list <int> synlist[2001];
- list <int> antlist[2001];
- int n, i, j, m, num= 0,synarr[2001],antarr[2001],k= 0,l= 0;
- bool synstore[2001][2001]= {0}, antstore[2001][2001]= {0};
- char s[1000],s1[30],s2[30];
- scanf("%d%d",&n,&m);
- getchar();
- while (n--)
- {
- scanf("%[^\n]",s);
- //puts(s);
- sscanf(s,"%s %s",s1,s2);
- //puts(s1);
- //puts(s2);
- if (!id[s1]) id[s1]= ++num;
- synarr[++k]= num;
- if (!id[s2]) id[s2]= ++num;
- synarr[++k]= num;
- synlist[ id[s1] ].push_back(id[s2]);
- synlist[ id[s2] ].push_back(id[s1]);
- synstore[ id[s1] ][ id[s2] ]= 1;
- synstore[ id[s2] ][ id[s1] ]= 1;
- word[ id[s1] ]= s1;
- word[ id[s2] ]= s2;
- getchar();
- }
- while (m--)
- {
- scanf("%[^\n]",s);
- sscanf(s,"%s %s",s1,s2);
- //puts(s);
- //puts(s1);
- //puts(s2);
- if (!id[s1]) id[s1]= ++num;
- antarr[++l]= num;
- if (!id[s2]) id[s2]= ++num;
- antarr[++l]= num;
- antlist[ id[s1] ].push_back(id[s2]);
- antlist[ id[s2] ].push_back(id[s1]);
- antstore[ id[s1] ][ id[s2] ]= 1;
- antstore[ id[s2] ][ id[s1] ]= 1;
- word[ id[s1] ]= s1;
- word[ id[s2] ]= s2;
- getchar();
- }
- int start,end;
- bool flag= 0;
- for (i=1; i<=l; ++i)
- {
- start= antarr[i];
- list <int> ::iterator it;
- list <int> ::iterator it2;
- for (it= antlist[start].begin(); it!= antlist[start].end(); ++it)
- {
- for (it2= antlist[*it].begin(); it2!= antlist[*it].end(); ++it2)
- {
- synlist[start].push_back(*it2);
- synlist[*it2].push_back(start);
- synstore[start][*it2]= 1;
- synstore[*it2][start]= 1;
- if (antstore[start][*it2])
- {
- flag= 1;
- break;
- }
- }
- if (flag) break;
- }
- if (flag) break;
- }
- if (!flag)
- {
- bool index[4001]= {0};
- int nodes= 0, set[4001], p= 0;
- for (i=1; i<=k; ++i)
- {
- start= synarr[i];
- if (index[start]) continue;
- p= 0;
- index[start]= 1;
- set[++p]= start;
- nodes++;
- stack <int> act;
- stack <int> wait;
- wait.push(start);
- do
- {
- while (!wait.empty())
- {
- act.push( wait.top() );
- wait.pop();
- }
- while ( !act.empty() )
- {
- start= act.top();
- act.pop();
- list <int> ::iterator it;
- for (it= synlist[start].begin(); it!= synlist[start].end(); ++it)
- {
- if (index[*it]) continue;
- index[*it]= 1;
- set[++p]= *it;
- wait.push(*it);
- nodes++;
- if (nodes==k) break;
- }
- if (nodes==k) break;
- }
- if (nodes==k) break;
- }
- while (!wait.empty());
- //for (j=1; j<=p; ++j) cout << word[ set[j] ] << "<<" ;
- //puts("");
- for (j=1; j<=p; ++j)
- {
- for (l=1; l<=p; ++l)
- {
- if (j==l) continue;
- synstore[ set[j] ][ set[l] ]= 1;
- synstore[ set[l] ][ set[j] ]= 1;
- //cout << word[set[j]] << " " << word[set[l]] << endl;
- if (antstore[ set[j] ][ set[l] ])
- {
- //cout << word[set[j]] << " " << word[set[l]] << endl;
- flag= 1;
- break;
- }
- }
- if (flag) break;
- }
- if (flag || nodes==k) break;
- }
- }
- // outputs
- printf("Case %d: ",x);
- if (flag) puts("NO");
- else puts("YES");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement