#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
void test_case() {
int n; cin >> n;
map<string, int> mp;
vector<int> a(n);
vector<vector<int>> adj2(n);
vector<int> indeg(n);
for (int i = 0; i < n; i++) {
string s, t;
cin >> s >> t;
if (!mp.count(s)) mp[s] = mp.size();
if (!mp.count(t)) mp[t] = mp.size();
a[mp[s]] = mp[t];
adj2[mp[s]].push_back(mp[t]);
adj2[mp[t]].push_back(mp[s]);
indeg[mp[t]]++;
}
if (n & 1) {
cout << -1 << '\n'; return;
}
vector<bool> vis(n), dir(n), vis3(n), in_ccl(n);
for (int i = 0; i < n; i++) {
if (i != a[i] && a[a[i]] == i) {
dir[i] = dir[a[i]] = 1;
}
}
int ans = 0, extra = 0;
for (int i = 0; i < n; i++) if (!vis[i]) {
vector<int> got;
queue<int> q;
auto dfs = [&](auto&& s, int v) -> void {
got.emplace_back(v);
vis[v] = 1;
if (!indeg[v]) q.push(v);
for (int u : adj2[v]) if (!vis[u]) s(s, u);
};
dfs(dfs, i);
while (q.size()) {
int v = q.front(); q.pop();
if (dir[v]) {
if (--indeg[a[v]] == 0) q.push(a[v]);
continue;
}
int nxt = a[v];
if (!dir[nxt]) {
dir[nxt] = dir[v] = 1;
ans++;
}
else {
dir[v] = 1;
extra++;
// we need to find someone else
}
if (--indeg[nxt] == 0) q.push(nxt);
}
int to = i;
while (!vis3[to]) vis3[to] = 1, to = a[to];
while (!in_ccl[to]) in_ccl[to] = 1, to = a[to];
int starting = -1, sx = -1;
for (int v : got) if (in_ccl[v]) {
sx = v;
if (dir[v] && !dir[a[v]]) {
starting = v; break;
}
}
// if we dont have, then arbitrary is fine
assert(~sx);
if (!~starting) starting = sx;
int v = starting;
for (int i = 0; i < sz(got); i++, v = a[v]) if (!dir[v]) {
int nxt = a[v];
if (v == nxt) {
dir[v] = 1;
extra++; continue;
}
if (!dir[nxt]) {
dir[nxt] = dir[v] = 1;
ans += a[nxt] != v;
}
else {
dir[v] = 1;
extra++;
}
}
}
cout << ans + extra << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
251 ms |
17272 KB |
Output is correct |
5 |
Correct |
271 ms |
15060 KB |
Output is correct |
6 |
Correct |
246 ms |
17548 KB |
Output is correct |
7 |
Correct |
252 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
261 ms |
14672 KB |
Output is correct |
2 |
Correct |
244 ms |
17756 KB |
Output is correct |
3 |
Correct |
218 ms |
16948 KB |
Output is correct |
4 |
Correct |
228 ms |
16720 KB |
Output is correct |
5 |
Correct |
236 ms |
18660 KB |
Output is correct |
6 |
Correct |
261 ms |
17552 KB |
Output is correct |
7 |
Correct |
261 ms |
17104 KB |
Output is correct |
8 |
Correct |
273 ms |
16724 KB |
Output is correct |
9 |
Correct |
232 ms |
17428 KB |
Output is correct |
10 |
Correct |
207 ms |
17848 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
356 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
251 ms |
17272 KB |
Output is correct |
20 |
Correct |
271 ms |
15060 KB |
Output is correct |
21 |
Correct |
246 ms |
17548 KB |
Output is correct |
22 |
Correct |
252 ms |
14444 KB |
Output is correct |
23 |
Correct |
261 ms |
14672 KB |
Output is correct |
24 |
Correct |
244 ms |
17756 KB |
Output is correct |
25 |
Correct |
218 ms |
16948 KB |
Output is correct |
26 |
Correct |
228 ms |
16720 KB |
Output is correct |
27 |
Correct |
236 ms |
18660 KB |
Output is correct |
28 |
Correct |
261 ms |
17552 KB |
Output is correct |
29 |
Correct |
261 ms |
17104 KB |
Output is correct |
30 |
Correct |
273 ms |
16724 KB |
Output is correct |
31 |
Correct |
232 ms |
17428 KB |
Output is correct |
32 |
Correct |
207 ms |
17848 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 |
348 KB |
Output is correct |
37 |
Correct |
268 ms |
17056 KB |
Output is correct |
38 |
Correct |
246 ms |
17648 KB |
Output is correct |
39 |
Correct |
255 ms |
16728 KB |
Output is correct |
40 |
Correct |
240 ms |
17428 KB |
Output is correct |
41 |
Correct |
239 ms |
16852 KB |
Output is correct |
42 |
Correct |
263 ms |
17360 KB |
Output is correct |
43 |
Correct |
249 ms |
17104 KB |
Output is correct |
44 |
Correct |
300 ms |
17248 KB |
Output is correct |
45 |
Correct |
246 ms |
17340 KB |
Output is correct |
46 |
Correct |
265 ms |
17292 KB |
Output is correct |
47 |
Correct |
226 ms |
17760 KB |
Output is correct |
48 |
Correct |
264 ms |
16728 KB |
Output is correct |
49 |
Correct |
246 ms |
17832 KB |
Output is correct |
50 |
Correct |
209 ms |
16736 KB |
Output is correct |
51 |
Correct |
244 ms |
16592 KB |
Output is correct |
52 |
Correct |
275 ms |
18564 KB |
Output is correct |
53 |
Correct |
236 ms |
17432 KB |
Output is correct |
54 |
Correct |
242 ms |
16724 KB |
Output is correct |
55 |
Correct |
247 ms |
16728 KB |
Output is correct |
56 |
Correct |
227 ms |
17540 KB |
Output is correct |
57 |
Correct |
161 ms |
17912 KB |
Output is correct |
58 |
Correct |
1 ms |
344 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 |
348 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 |
265 ms |
19328 KB |
Output is correct |
66 |
Correct |
271 ms |
16580 KB |
Output is correct |
67 |
Correct |
243 ms |
19916 KB |
Output is correct |
68 |
Correct |
246 ms |
16776 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
344 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 |
348 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 |
460 KB |
Output is correct |
80 |
Correct |
1 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 |