uva 10158 War
四个操作:1)使AB成为朋友。2)使AB成为敌人。3)询问AB是不是朋友。4)询问AB是不是敌人。PS:敌人的敌人是朋友。朋友的朋友是朋友。
开一个2 * n的数组,0~n - 1表示的是朋友,n~2 * n - 1表示的是n号国家的敌人。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
const int N = 100005;
typedef long long ll;
int n, f[N * 2];
void init() {
for (int i = 0; i < n; i++) {
f[i] = i;
f[i + n] = i + n;
}
}
int find(int x) {
return x == f[x] ? x : f[x] = find(f[x]);
}
void setFriends(int x, int y) {
int px = find(x), py = find(y);
int qx = find(x + n), qy = find(y + n);
if (px == qy || qx == py) {
printf("-1\n");
return;
}
f[px] = py;
f[qx] = qy;
}
void setEnemies(int x, int y) {
int px = find(x), py = find(y);
int qx = find(x + n), qy = find(y + n);
if (px == py || qx == qy) {
printf("-1\n");
return;
}
f[px] = qy;
f[qx] = py;
}
void areFriends(int x, int y) {
int px = find(x), py = find(y);
if (px == py) printf("1\n");
else printf("0\n");
}
void areEnemies(int x, int y) {
int px = find(x), py = find(y + n);
if (px == py) printf("1\n");
else printf("0\n");
}
int main() {
scanf("%d", &n);
int a, b, c;
init();
while (scanf("%d %d %d", &a, &b, &c) == 3) {
if (!a && !b && !c) break;
switch(a) {
case 1: setFriends(b, c);
break;
case 2: setEnemies(b, c);
break;
case 3: areFriends(b, c);
break;
case 4: areEnemies(b, c);
break;
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许也可以转载,不过要注明出处哦。
时间: 2024-10-25 05:35:06