-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathabandonedanimal.cc
More file actions
63 lines (58 loc) · 1.05 KB
/
Copy pathabandonedanimal.cc
File metadata and controls
63 lines (58 loc) · 1.05 KB
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
// https://open.kattis.com/problems/abandonedanimal
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef vector<int> vi;
typedef vector<string> vs;
typedef unordered_map<string, int> msi;
typedef unordered_map<int, int> mii;
typedef vector<vi> vvi;
typedef vector<mii> vmi;
vmi cache;
int s(const vvi &a, const vi& p, int i, int j) {
if (i == p.size()) return 1;
if (cache[i].count(j)) return cache[i][j];
int c = 0;
for (auto k : a[p[i]])
if (k >= j) c += s(a, p, i + 1, k);
cache[i][j] = c;
return c;
}
int main() {
int n, k, q;
cin >> n >> k;
vvi a;
vs mi;
msi m;
for (int i = 0; i < k; i++) {
int x;
string s;
cin >> x >> s;
if (!m.count(s)) {
m[s] = mi.size();
mi.push_back(s);
a.push_back(vi());
}
a[m[s]].push_back(x);
}
cin >> q;
cache = vmi(q);
vi p(q);
for (int i = 0; i < q; i++) {
string s;
cin >> s;
p[i] = m[s];
}
switch (s(a, p, 0, 0)) {
case 0:
cout << "impossible\n";
break;
case 1:
cout << "unique\n";
break;
default:
cout << "ambiguous\n";
break;
}
}