#include <bits/stdc++.h>
#define ALL(X) begin(X), end(X)
using namespace std;
using i64 = long long;
template <class T>
using vec = vector<T>;
vec<int> get_input(int n) {
unordered_map<string, int> id;
auto get_id = [&](const string& str) -> int {
if (auto it = id.find(str); it != end(id))
return it->second;
int sz = size(id);
return id[str] = sz;
};
vec<int> ret(n);
for (int i = 0; i < n; i++) {
string s, t; cin >> s >> t;
ret[get_id(s)] = get_id(t);
}
return ret;
}
constexpr int inf = 1e9;
void solve1(int n) {
if (n % 2 == 1)
return void(cout << "-1\n");
vec<int> to = get_input(n);
vec<int> dp(1 << n, inf);
dp[0] = 0;
for (int mask = 1; mask < (1 << n); mask++) if (__builtin_popcount(mask) % 2 == 0) {
for (int i = 0; i < n; i++) if ((mask >> i) & 1) {
for (int j = 0; j < i; j++) if ((mask >> j) & 1) {
dp[mask] = min(dp[mask], dp[mask ^ (1 << i) ^ (1 << j)] + (to[i] != j) + (to[j] != i));
}
}
}
cout << dp.back() << '\n';
}
void solve2(int n) {
if (n % 2 == 1)
return void(cout << "-1\n");
vec<int> to = get_input(n);
vec<bool> is_cir(n);
vec<int> indeg(n), rt(n), sz(n);
for (int i = 0; i < n; i++)
++indeg[to[i]];
vec<vec<int>> G(n);
{
queue<int> q;
for (int i = 0; i < n; i++)
if (indeg[i] == 0)
q.emplace(i);
while (!empty(q)) {
int x = q.front(); q.pop();
G[to[x]].emplace_back(x);
if (--indeg[to[x]] == 0)
q.emplace(to[x]);
}
}
vec<array<int, 2>> max_pair(n);
for (int i = 0; i < n; i++) if (indeg[i]) {
for (int x = i; indeg[x]; x = to[x]) {
is_cir[x] = true;
indeg[x] = 0;
++sz[i];
auto dfs = [&](auto&& f, int y) -> void {
rt[y] = x;
for (auto nxt : G[y]) {
f(f, nxt);
max_pair[y][0] += max_pair[nxt][1];
}
for (auto nxt : G[y]) {
max_pair[y][1] = max(max_pair[y][1], max_pair[y][0] - max_pair[nxt][1] + max_pair[nxt][0] + 1);
}
};
dfs(dfs, x);
rt[x] = i;
}
}
vec<array<int, 2>> dp0(n), dp1(n);
vec<int> ans(n);
for (int i = 0; i < n; i++) if (rt[i] == i) {
if (sz[i] == 2) {
ans[i] = 2 + max_pair[i][0] + max_pair[to[i]][0];
}
dp0[i][0] = max_pair[i][0];
dp0[i][1] = -inf;
dp1[i][0] = max_pair[i][0];
dp1[i][1] = max_pair[i][1];
int pre = i;
for (int x = to[i], cnt = 1; cnt < sz[i]; x = to[x], ++cnt) {
dp1[x][0] = max_pair[x][0] + dp1[pre][1];
dp1[x][1] = max(max_pair[x][1] + dp1[pre][1], max_pair[x][0] + dp1[pre][0] + 1);
if (pre != i) {
dp0[x][0] = max_pair[x][0] + dp0[pre][1];
dp0[x][1] = max(max_pair[x][1] + dp0[pre][1], max_pair[x][0] + dp0[pre][0] + 1);
}
else {
dp0[x][0] = max_pair[x][0] + dp0[pre][0];
dp0[x][1] = max(max_pair[x][1] + dp0[pre][0], max_pair[x][0] + dp0[pre][0]);
}
if (cnt == sz[i] - 1) {
ans[i] = max({ans[i], dp0[x][1], dp1[x][0], dp1[x][1]});
ans[i] = max(ans[i], dp0[x][0] + 1);
}
pre = x;
}
if (sz[i] == 1)
ans[i] = max(max_pair[i][0], max_pair[i][1]);
}
int tot{};
for (int i = 0; i < n; i++)
if (rt[i] == i)
tot += ans[i];
cout << n - tot << '\n';
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n; cin >> n;
/*if (n <= 20) solve1(n);
else */solve2(n);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
34 ms |
10472 KB |
Output is correct |
5 |
Correct |
36 ms |
10448 KB |
Output is correct |
6 |
Correct |
33 ms |
10436 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
11836 KB |
Output is correct |
2 |
Correct |
49 ms |
12668 KB |
Output is correct |
3 |
Correct |
27 ms |
10448 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
45 ms |
15768 KB |
Output is correct |
6 |
Correct |
37 ms |
11136 KB |
Output is correct |
7 |
Correct |
37 ms |
11132 KB |
Output is correct |
8 |
Correct |
36 ms |
10624 KB |
Output is correct |
9 |
Correct |
33 ms |
10448 KB |
Output is correct |
10 |
Correct |
28 ms |
10792 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
34 ms |
10472 KB |
Output is correct |
20 |
Correct |
36 ms |
10448 KB |
Output is correct |
21 |
Correct |
33 ms |
10436 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
40 ms |
11836 KB |
Output is correct |
24 |
Correct |
49 ms |
12668 KB |
Output is correct |
25 |
Correct |
27 ms |
10448 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
45 ms |
15768 KB |
Output is correct |
28 |
Correct |
37 ms |
11136 KB |
Output is correct |
29 |
Correct |
37 ms |
11132 KB |
Output is correct |
30 |
Correct |
36 ms |
10624 KB |
Output is correct |
31 |
Correct |
33 ms |
10448 KB |
Output is correct |
32 |
Correct |
28 ms |
10792 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
39 ms |
12000 KB |
Output is correct |
38 |
Correct |
39 ms |
10696 KB |
Output is correct |
39 |
Correct |
38 ms |
10812 KB |
Output is correct |
40 |
Correct |
39 ms |
11144 KB |
Output is correct |
41 |
Correct |
38 ms |
11132 KB |
Output is correct |
42 |
Correct |
40 ms |
11388 KB |
Output is correct |
43 |
Correct |
45 ms |
11644 KB |
Output is correct |
44 |
Correct |
39 ms |
11388 KB |
Output is correct |
45 |
Correct |
43 ms |
11608 KB |
Output is correct |
46 |
Correct |
38 ms |
11392 KB |
Output is correct |
47 |
Correct |
33 ms |
10620 KB |
Output is correct |
48 |
Correct |
39 ms |
11900 KB |
Output is correct |
49 |
Correct |
43 ms |
12608 KB |
Output is correct |
50 |
Correct |
26 ms |
10448 KB |
Output is correct |
51 |
Correct |
0 ms |
344 KB |
Output is correct |
52 |
Correct |
40 ms |
15720 KB |
Output is correct |
53 |
Correct |
37 ms |
11068 KB |
Output is correct |
54 |
Correct |
36 ms |
11136 KB |
Output is correct |
55 |
Correct |
35 ms |
10620 KB |
Output is correct |
56 |
Correct |
33 ms |
10448 KB |
Output is correct |
57 |
Correct |
32 ms |
11040 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
456 KB |
Output is correct |
62 |
Correct |
0 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
35 ms |
10396 KB |
Output is correct |
66 |
Correct |
32 ms |
10436 KB |
Output is correct |
67 |
Correct |
34 ms |
10600 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
456 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
456 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
456 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |