#include <bits/stdc++.h>
using namespace std;
const int N = 100'007;
int n, p[N];
vector<int> adj[N];
map<string, int> id;
int get_id(const string &s) {
return id.count(s) ? id[s] : id[s] = (int) id.size();
}
int vis[N];
pair<int, int> dfs(int u, int limit) {
vis[u] = limit;
int a = 0, b = 0;
for (int v : adj[u]) if (vis[v] < limit) {
auto [c, d] = dfs(v, limit);
a += d;
b = max(b, d - c);
}
// cerr << u << ": " << a << ' ' << a - b + 1 << '\n';
return {a, a - b + 1};
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
if (n & 1) {
cout << "-1\n";
return 0;
}
for (int i = 0; i < n; i++) {
string a, b; cin >> a >> b;
int u = get_id(a), v = get_id(b);
p[u] = v;
adj[u].push_back(v);
adj[v].push_back(u);
}
int ans = 0;
for (int i = 0; i < n; i++) if (!vis[i]) {
int u = i;
while (!vis[u]) {
vis[u] = 1;
u = p[u];
}
if (u == p[u]) {
// cerr << "single meduse: " << u << '\n';
ans += dfs(u, 3).second;
continue;
}
int v = p[u];
adj[u].erase(find(adj[u].begin(), adj[u].end(), v));
adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
int res = 1;
vis[u] = vis[v] = 2;
for (int neigh : adj[u]) if (vis[neigh] != 2) {
res += dfs(neigh, 2).second;
}
for (int neigh : adj[v]) if (vis[neigh] != 2) {
res += dfs(neigh, 2).second;
}
// cerr << "cost in: " << u << ' ' << min(res - (u == p[p[u]]), dfs(u, 3).second) << '\n';
ans += min(res - (u == p[p[u]]), dfs(u, 3).second);
}
cout << ans << '\n';
}
Compilation message
polygon.cpp: In function 'std::pair<int, int> dfs(int, int)':
polygon.cpp:24:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
24 | auto [c, d] = dfs(v, limit);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2592 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2684 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2704 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
233 ms |
21976 KB |
Output is correct |
5 |
Correct |
210 ms |
16512 KB |
Output is correct |
6 |
Correct |
247 ms |
22712 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
238 ms |
16572 KB |
Output is correct |
2 |
Correct |
207 ms |
17928 KB |
Output is correct |
3 |
Correct |
151 ms |
16460 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
270 ms |
22348 KB |
Output is correct |
6 |
Correct |
194 ms |
16648 KB |
Output is correct |
7 |
Correct |
203 ms |
16664 KB |
Output is correct |
8 |
Correct |
219 ms |
16460 KB |
Output is correct |
9 |
Correct |
173 ms |
16856 KB |
Output is correct |
10 |
Correct |
143 ms |
16968 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2684 KB |
Output is correct |
13 |
Correct |
2 ms |
2588 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2592 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2684 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2684 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2704 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2680 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
233 ms |
21976 KB |
Output is correct |
20 |
Correct |
210 ms |
16512 KB |
Output is correct |
21 |
Correct |
247 ms |
22712 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
238 ms |
16572 KB |
Output is correct |
24 |
Correct |
207 ms |
17928 KB |
Output is correct |
25 |
Correct |
151 ms |
16460 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
270 ms |
22348 KB |
Output is correct |
28 |
Correct |
194 ms |
16648 KB |
Output is correct |
29 |
Correct |
203 ms |
16664 KB |
Output is correct |
30 |
Correct |
219 ms |
16460 KB |
Output is correct |
31 |
Correct |
173 ms |
16856 KB |
Output is correct |
32 |
Correct |
143 ms |
16968 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
3 ms |
2684 KB |
Output is correct |
35 |
Correct |
2 ms |
2588 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
234 ms |
16680 KB |
Output is correct |
38 |
Correct |
233 ms |
17536 KB |
Output is correct |
39 |
Correct |
246 ms |
16588 KB |
Output is correct |
40 |
Correct |
207 ms |
16596 KB |
Output is correct |
41 |
Correct |
202 ms |
16656 KB |
Output is correct |
42 |
Correct |
248 ms |
16720 KB |
Output is correct |
43 |
Correct |
209 ms |
16660 KB |
Output is correct |
44 |
Correct |
233 ms |
16604 KB |
Output is correct |
45 |
Correct |
235 ms |
16648 KB |
Output is correct |
46 |
Correct |
215 ms |
16588 KB |
Output is correct |
47 |
Correct |
227 ms |
16896 KB |
Output is correct |
48 |
Correct |
206 ms |
16460 KB |
Output is correct |
49 |
Correct |
224 ms |
17920 KB |
Output is correct |
50 |
Correct |
148 ms |
16492 KB |
Output is correct |
51 |
Correct |
2 ms |
2644 KB |
Output is correct |
52 |
Correct |
247 ms |
22252 KB |
Output is correct |
53 |
Correct |
233 ms |
16588 KB |
Output is correct |
54 |
Correct |
204 ms |
16624 KB |
Output is correct |
55 |
Correct |
205 ms |
16576 KB |
Output is correct |
56 |
Correct |
217 ms |
16744 KB |
Output is correct |
57 |
Correct |
139 ms |
16840 KB |
Output is correct |
58 |
Correct |
2 ms |
2644 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2680 KB |
Output is correct |
61 |
Correct |
2 ms |
2644 KB |
Output is correct |
62 |
Correct |
2 ms |
2644 KB |
Output is correct |
63 |
Correct |
2 ms |
2644 KB |
Output is correct |
64 |
Correct |
2 ms |
2644 KB |
Output is correct |
65 |
Correct |
273 ms |
22048 KB |
Output is correct |
66 |
Correct |
202 ms |
16544 KB |
Output is correct |
67 |
Correct |
256 ms |
22732 KB |
Output is correct |
68 |
Correct |
2 ms |
2696 KB |
Output is correct |
69 |
Correct |
2 ms |
2644 KB |
Output is correct |
70 |
Correct |
2 ms |
2684 KB |
Output is correct |
71 |
Correct |
2 ms |
2684 KB |
Output is correct |
72 |
Correct |
2 ms |
2644 KB |
Output is correct |
73 |
Correct |
2 ms |
2644 KB |
Output is correct |
74 |
Correct |
1 ms |
2680 KB |
Output is correct |
75 |
Correct |
2 ms |
2644 KB |
Output is correct |
76 |
Correct |
2 ms |
2684 KB |
Output is correct |
77 |
Correct |
2 ms |
2644 KB |
Output is correct |
78 |
Correct |
1 ms |
2684 KB |
Output is correct |
79 |
Correct |
1 ms |
2644 KB |
Output is correct |
80 |
Correct |
2 ms |
2644 KB |
Output is correct |
81 |
Correct |
2 ms |
2644 KB |
Output is correct |
82 |
Correct |
1 ms |
2644 KB |
Output is correct |
83 |
Correct |
2 ms |
2644 KB |
Output is correct |