Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- using namespace std;
- int n, m, q, T, Ant[100005], F1, F2, t[100005], c[100005], Fant1, Fant2;
- string s, s1, s2;
- map<string, int> M;
- int Find( int x )
- {
- int r, y;
- for( r = x; r != t[ x ]; r = t[x] );
- while( x != t[ x ])
- {
- y = t[ x ];
- t[ x ] = r;
- x = y;
- }
- return r;
- }
- void Unite( int x, int y )
- {
- if( c[ y ] <= c[ x ] )
- t[ y ] = x;
- else
- t[ x ] = y;
- if( c[ x ] == c[ y ] ) c[ x ] ++;
- }
- int main()
- {
- cin >> n >> m >> q;
- for( int i = 1; i <= n; ++ i )
- {
- cin >> s;
- M[ s ] = i;
- Ant[ i ] = n + i;
- Ant[ n + i ] = i;
- t[ i ] = i;
- c[ i ] = 1;
- t[ n + i ] = n+i;
- c[ n + i ] = 1;
- }
- for( int i = 1; i <= m ; ++ i )
- {
- cin >> T;
- cin >> s1;
- cin >> s2;
- F1 = Find( M[ s1 ] );
- F2 = Find( M[ s2 ] );
- Fant2 = Find( Ant[ F2 ] );
- Fant1 = Find( Ant[ F1 ] );
- if( T == 1 )
- {
- if( F1 == Fant2 || F2 == Fant1 )
- {
- cout << "NO\n";
- continue;
- }
- Unite( F1, F2 );
- Unite( Fant1, Fant2 );
- cout << "YES\n";
- continue;
- }
- if( F1 == F2 )
- {
- cout << "NO\n";
- continue;
- }
- Unite( F1, Fant2 );
- Unite( Fant1, F2 );
- cout << "YES\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement