forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrandopening.cpp
85 lines (83 loc) · 2.01 KB
/
grandopening.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
vector<int> p(1000001,-1);//change size here if needed
int find(int x) {return p[x] < 0 ? x : p[x] = find(p[x]);}
void merge(int x, int y) {
if((x=find(x)) == (y=find(y))) return;
if(p[y] < p[x]) swap(x,y);
p[x] += p[y];
p[y] = x;
}
int main() {
int n, m;
cin >> n >> m;
vector<string> animal(n);
unordered_map<string, vector<string>> adj;
unordered_map<string, int> deg;//in-out
unordered_map<string, int> toID;
int found = 0;
for(int i = 0; i < n; ++i) {
cin >> animal[i];
toID[animal[i]] = i;
int size;
cin >> size;
for(int j = 0; j < size; ++j) {
string to;
cin >> to;
if(to == animal[i]) continue;
++found;
adj[animal[i]].push_back(to);
++deg[to];
--deg[animal[i]];
}
}
if(found == 0) {
cout << "FALSE ALARM\n";
return 0;
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < adj[animal[i]].size(); ++j) {
string to = adj[animal[i]][j];
int u = toID[to];
int v = toID[animal[i]];
merge(u,v);
}
}
int CC = 0;
for(int i = 0; i < n; ++i) {
if(-p[i] == 1) continue;
if(p[i] < 0)
++CC;
}
if(CC > 1) {
cout << "IMPOSSIBLE\n";
return 0;
}
string start = "";
int ones = 0, negOnes = 0;
for(int i = 0; i < n; ++i) {
if(deg[animal[i]] > 1) {
cout << "IMPOSSIBLE\n";
return 0;
}
if(deg[animal[i]] < -1) {
cout << "IMPOSSIBLE\n";
return 0;
}
if(deg[animal[i]] == 1) {
++ones;
}
if(deg[animal[i]] == -1) {
++negOnes;
}
}
if(ones == 1 && negOnes == 1) {
cout << "POSSIBLE\n";
return 0;
}
if(ones == 0 && negOnes == 0) {
cout << "POSSIBLE\n";
return 0;
}
cout << "IMPOSSIBLE\n";
}