-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10158.java
108 lines (80 loc) · 1.64 KB
/
10158.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* UVa 10158: War
*
* Problem Statement: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1099
*/
import java.util.Scanner;
public class Main {
static int n;
static int[] p;
static void init(int N) {
n = N;
p = new int[n * 2];
for (int i = 0; i < n * 2; i++) {
p[i] = i;
}
}
static int find(int x) {
if (x != p[x]) {
p[x] = find(p[x]);
return p[x];
}
return x;
}
static boolean areFriends(int x1, int y1) {
return x1 == y1;
}
static boolean areEnemies(int x1, int y2) {
return x1 == y2;
}
static void setFriends(int x1, int x2, int y1, int y2) {
if (x1 == y2) {
System.out.println("-1");
return;
}
p[x1] = y1;
p[x2] = y2;
}
static void setEnemies(int x1, int x2, int y1, int y2) {
if (x1 == y1) {
System.out.println("-1");
return;
}
p[x1] = y2;
p[x2] = y1;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
init(scan.nextInt());
int c = scan.nextInt();
int x = scan.nextInt();
int y = scan.nextInt();
int x1, x2, y1, y2;
while (c != 0) {
x1 = find(x);
x2 = find(x + n);
y1 = find(y);
y2 = find(y + n);
if (c == 1)
setFriends(x1, x2, y1, y2);
if (c == 2)
setEnemies(x1, x2, y1, y2);
if (c == 3) {
if (areFriends(x1, y1))
System.out.println("1");
else
System.out.println("0");
}
if (c == 4) {
if (areEnemies(x1, y2))
System.out.println("1");
else
System.out.println("0");
}
c = scan.nextInt();
x = scan.nextInt();
y = scan.nextInt();
}
scan.close();
}
}