#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <map>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
std::map<std::string, int> mp;
int const nmax = 200000;
int v[1 + nmax], used[1 + nmax];
std::vector<int> g[1 + nmax];
int frec[1 + nmax];
int dp[1 + nmax][2];
void dfs(int node) {
used[node] = 1;
for(int h = 0; h < g[node].size(); h++) {
int to = g[node][h];
if(0 == frec[to]) {
dfs(to);
int a = dp[node][0] + std::max(dp[to][0], dp[to][1]);
int b = std::max(dp[node][1] + std::max(dp[to][1], dp[to][0]), dp[node][0] + dp[to][0] + 1);
dp[node][0] = a;
dp[node][1] = b;
}
}
}
int quick(std::vector<int> path) {
std::vector<std::pair<int,int>> dp2;
int n = path.size();
dp2.resize(n);
int coef = 1;
if(path.size() == 2)
coef = 2;
for(int i = 0; i < path.size(); i++) {
int node = path[i];
if(i == 0) {
dp2[i].first = dp[node][0];
dp2[i].second = dp[node][1];
} else {
dp2[i].first = std::max(dp2[i - 1].first, dp2[i - 1].second) + dp[node][0];
dp2[i].second = std::max(std::max(dp2[i - 1].first, dp2[i - 1].second) + dp[node][1],
dp2[i - 1].first + dp[node][0] + coef);
}
}
return std::max(dp2[n - 1].first, dp2[n - 1].second);
}
int solve(int node) {
std::vector<int> path;
while(frec[node] == 0) {
path.push_back(node);
frec[node] = path.size();
node = v[node];
}
std::vector<int> realpath;
for(int i = 0; i < frec[node] - 1; i++)
frec[path[i]] = 0;
for(int i = frec[node] - 1; i < path.size(); i++)
realpath.push_back(path[i]);
for(int i = 0; i < realpath.size(); i++)
dfs(realpath[i]);
int result = quick(realpath);
realpath.push_back(realpath[0]);
realpath.erase(realpath.begin());
return std::max(result, quick(realpath));
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n;
std::cin >> n;
if(n % 2 == 1) {
std::cout << -1;
return 0;
}
int ptr = 0;
for(int i = 1;i <= n; i++) {
std::string s, s2;
std::cin >> s >> s2;
if(0 == mp[s])
mp[s] = ++ptr;
if(0 == mp[s2])
mp[s2] = ++ptr;
v[mp[s]] = mp[s2];
}
int result = 0;
for(int i = 1;i <= n; i++) {
if(v[i] != i)
g[v[i]].push_back(i);
}
for(int i = 1;i <= n; i++)
if(used[i] == 0)
result += solve(i);
std::cout << n - result;
return 0;
}
Compilation message
polygon.cpp: In function 'void dfs(int)':
polygon.cpp:20:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int h = 0; h < g[node].size(); h++) {
| ~~^~~~~~~~~~~~~~~~
polygon.cpp: In function 'int quick(std::vector<int>)':
polygon.cpp:39:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < path.size(); i++) {
| ~~^~~~~~~~~~~~~
polygon.cpp: In function 'int solve(int)':
polygon.cpp:63:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = frec[node] - 1; i < path.size(); i++)
| ~~^~~~~~~~~~~~~
polygon.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 0; i < realpath.size(); i++)
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
3 ms |
4992 KB |
Output is correct |
4 |
Correct |
268 ms |
19320 KB |
Output is correct |
5 |
Correct |
271 ms |
17352 KB |
Output is correct |
6 |
Correct |
268 ms |
19648 KB |
Output is correct |
7 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
17496 KB |
Output is correct |
2 |
Correct |
288 ms |
19192 KB |
Output is correct |
3 |
Correct |
237 ms |
14584 KB |
Output is correct |
4 |
Correct |
4 ms |
5120 KB |
Output is correct |
5 |
Correct |
278 ms |
23160 KB |
Output is correct |
6 |
Correct |
264 ms |
16760 KB |
Output is correct |
7 |
Correct |
256 ms |
16888 KB |
Output is correct |
8 |
Correct |
272 ms |
16504 KB |
Output is correct |
9 |
Correct |
244 ms |
16120 KB |
Output is correct |
10 |
Correct |
223 ms |
14836 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 KB |
Output is correct |
2 |
Correct |
4 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
3 ms |
4992 KB |
Output is correct |
5 |
Correct |
4 ms |
4992 KB |
Output is correct |
6 |
Correct |
3 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
4 ms |
4992 KB |
Output is correct |
10 |
Correct |
3 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
4 ms |
4992 KB |
Output is correct |
14 |
Correct |
3 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
3 ms |
4992 KB |
Output is correct |
19 |
Correct |
268 ms |
19320 KB |
Output is correct |
20 |
Correct |
271 ms |
17352 KB |
Output is correct |
21 |
Correct |
268 ms |
19648 KB |
Output is correct |
22 |
Correct |
4 ms |
4992 KB |
Output is correct |
23 |
Correct |
264 ms |
17496 KB |
Output is correct |
24 |
Correct |
288 ms |
19192 KB |
Output is correct |
25 |
Correct |
237 ms |
14584 KB |
Output is correct |
26 |
Correct |
4 ms |
5120 KB |
Output is correct |
27 |
Correct |
278 ms |
23160 KB |
Output is correct |
28 |
Correct |
264 ms |
16760 KB |
Output is correct |
29 |
Correct |
256 ms |
16888 KB |
Output is correct |
30 |
Correct |
272 ms |
16504 KB |
Output is correct |
31 |
Correct |
244 ms |
16120 KB |
Output is correct |
32 |
Correct |
223 ms |
14836 KB |
Output is correct |
33 |
Correct |
4 ms |
4992 KB |
Output is correct |
34 |
Correct |
3 ms |
4992 KB |
Output is correct |
35 |
Correct |
4 ms |
4992 KB |
Output is correct |
36 |
Correct |
4 ms |
4992 KB |
Output is correct |
37 |
Correct |
293 ms |
19812 KB |
Output is correct |
38 |
Correct |
274 ms |
19172 KB |
Output is correct |
39 |
Correct |
276 ms |
18680 KB |
Output is correct |
40 |
Correct |
266 ms |
18424 KB |
Output is correct |
41 |
Correct |
256 ms |
18428 KB |
Output is correct |
42 |
Correct |
263 ms |
19064 KB |
Output is correct |
43 |
Correct |
268 ms |
19192 KB |
Output is correct |
44 |
Correct |
264 ms |
19064 KB |
Output is correct |
45 |
Correct |
270 ms |
19028 KB |
Output is correct |
46 |
Correct |
274 ms |
18940 KB |
Output is correct |
47 |
Correct |
250 ms |
17928 KB |
Output is correct |
48 |
Correct |
273 ms |
19320 KB |
Output is correct |
49 |
Correct |
287 ms |
20896 KB |
Output is correct |
50 |
Correct |
243 ms |
16240 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
270 ms |
25044 KB |
Output is correct |
53 |
Correct |
256 ms |
18204 KB |
Output is correct |
54 |
Correct |
268 ms |
18680 KB |
Output is correct |
55 |
Correct |
253 ms |
18296 KB |
Output is correct |
56 |
Correct |
256 ms |
17784 KB |
Output is correct |
57 |
Correct |
195 ms |
16372 KB |
Output is correct |
58 |
Correct |
4 ms |
4992 KB |
Output is correct |
59 |
Correct |
4 ms |
4992 KB |
Output is correct |
60 |
Correct |
3 ms |
4992 KB |
Output is correct |
61 |
Correct |
3 ms |
4992 KB |
Output is correct |
62 |
Correct |
4 ms |
4992 KB |
Output is correct |
63 |
Correct |
4 ms |
4992 KB |
Output is correct |
64 |
Correct |
4 ms |
4992 KB |
Output is correct |
65 |
Correct |
290 ms |
21248 KB |
Output is correct |
66 |
Correct |
275 ms |
19320 KB |
Output is correct |
67 |
Correct |
273 ms |
21620 KB |
Output is correct |
68 |
Correct |
4 ms |
5120 KB |
Output is correct |
69 |
Correct |
3 ms |
5120 KB |
Output is correct |
70 |
Correct |
3 ms |
4992 KB |
Output is correct |
71 |
Correct |
3 ms |
4992 KB |
Output is correct |
72 |
Correct |
4 ms |
4992 KB |
Output is correct |
73 |
Correct |
4 ms |
5024 KB |
Output is correct |
74 |
Correct |
3 ms |
4992 KB |
Output is correct |
75 |
Correct |
3 ms |
4992 KB |
Output is correct |
76 |
Correct |
4 ms |
4992 KB |
Output is correct |
77 |
Correct |
3 ms |
4992 KB |
Output is correct |
78 |
Correct |
3 ms |
4992 KB |
Output is correct |
79 |
Correct |
4 ms |
4992 KB |
Output is correct |
80 |
Correct |
3 ms |
4992 KB |
Output is correct |
81 |
Correct |
3 ms |
4992 KB |
Output is correct |
82 |
Correct |
3 ms |
4992 KB |
Output is correct |
83 |
Correct |
4 ms |
4992 KB |
Output is correct |