This question is used to check the application of the set. It is a good question type.
I also refer to other people's ideas, but the idea is very simple.
I have mentioned a lot of complicated things about friends and enemies. You will find that, after reading them thoroughly, it is actually a sentence. If you are a friend of mine, then we have public enemies.
That is to say, there is no intersection between the people in this circle of friends and those in the circle of enemies;
But how do they represent friends or enemies?
N people, represented by 2 * n vertices, the first n represents itself, and the last n represents the separation of corresponding points. If they are friends, they add themselves. If they are enemies, add the separation.
When determining whether it is an enemy, it depends on whether the corresponding separation and the other itself are in the same set; If judging whether it is a friend, it depends on whether the two are in a different set.
The Code is as follows:
#include <cstdio>#include <cstring>const int N = 20020;int fa[N], n;int find ( int a ) { return fa[a] == a ? a : fa[a] = find(fa[a]);}void setFriend( int a, int b ) { int x1, x2, y1, y2; x1 = find(a), y1 = find(b); x2 = find(a+n), y2 = find(b+n); if ( x1 == y2 || x2 == y1 ) printf("-1\n"); else { fa[x1] = y1; fa[x2] = y2; }}void setEnemy( int a, int b ) { int x1, x2, y1, y2; x1 = find(a), y1 = find(b); x2 = find(a+n), y2 = find(b+n); if ( x1 == y1 ) printf("-1\n"); else { fa[x1] = y2; fa[y1] = x2; }}void areFriend( int a, int b ) { int x, y; x = find(a); y = find(b); if ( x == y ) printf("1\n"); else printf("0\n");}void areEnemy( int a, int b ) { int x1, x2, y1, y2; x1 = find(a), y1 = find(b); x2 = find(a+n), y2 = find(b+n); if ( x2 == y1 || x1 == y2 ) printf("1\n"); else printf("0\n");} int main(){ while ( scanf("%d", &n) != EOF ) { int c, a, b; for ( int i = 0; i <= n*2; ++i ) fa[i] = i; while ( 1 ) { scanf("%d%d%d", &c, &a, &b); if ( a == 0 && c == 0 && b == 0 ) break; if ( c == 1 ) setFriend( a, b ); else if ( c == 2 ) setEnemy( a, b ); else if ( c == 3 ) areFriend( a, b ); else if ( c == 4 ) areEnemy( a, b ); } } return 0;}