-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10596.cpp
68 lines (60 loc) · 1.3 KB
/
P10596.cpp
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
typedef pair<int,int> Edge;
#define C1 first
#define C2 second
int find(int x, int *parents) {
if(parents[x] != x)
parents[x] = find(parents[x], parents);
return parents[x];
}
bool _union(int x, int y, int *parents, int *ranks) {
int xRoot = find(x, parents);
int yRoot = find(y, parents);
if(xRoot == yRoot)
return false;
// x and y are not already in same set. Merge them.
if(ranks[xRoot] < ranks[yRoot])
parents[xRoot] = yRoot;
else if(ranks[xRoot] > ranks[yRoot])
parents[yRoot] = xRoot;
else {
parents[yRoot] = xRoot;
++ranks[xRoot];
}
return true;
}
int main() {
int parents[201], ranks[201], inDegrees[201];
int N, R, c1, c2;
while(cin >> N >> R) {
// Reset memory:
FORI(N) {
ranks[i] = inDegrees[i] = 0;
parents[i] = i;
}
int trees = N;
// Read edges:
FORI(R) {
cin >> c1 >> c2;
if(_union(c1, c2, parents, ranks))
--trees;
++inDegrees[c1];
++inDegrees[c2];
}
FORI(N) {
if(inDegrees[i] == 0)
--trees; // Ignore all non-visited intersections.
}
FORI(N) {
if(inDegrees[i] % 2 == 1) {
trees = -1;
//cerr << "Uneven degree!" << endl;
break;
}
}
if(trees == 1)
cout << "Possible" << endl;
else
cout << "Not Possible" << endl;
}
return 0;
}