#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int f[maxn];
int comp[maxn], nivel[maxn], cc;
int ini[maxn], fim[maxn];
int mark[maxn];
bool done[maxn], done2[maxn], incycle[maxn];
vector<int> all[maxn];
void dfs(int u)
{
mark[u] = 1;
if (mark[f[u]] == 1)
{
comp[u] = ++cc;
mark[u] = 2;
ini[cc] = f[u], fim[cc] = u;
return;
}
if (!mark[f[u]]) dfs(f[u]);
comp[u] = comp[f[u]], mark[u] = 2;
nivel[u] = nivel[f[u]]+1;
}
int solve_ciclo(int c)
{
if (f[fim[c]] == fim[c]) return 0;
for (auto u: all[c])
done2[u] = done[u];
int ans = 0;
if (f[ini[c]] == fim[c]) ans += 2;
else ans++;
done2[ini[c]] = done2[fim[c]] = 1;
for (auto u: all[c])
{
if (done2[u]) continue;
if (f[u] != u && !done2[f[u]])
{
ans++;
done2[u] = done2[f[u]] = 1;
}
}
return ans;
}
int solve_nociclo(int c)
{
for (auto u: all[c])
done2[u] = done[u];
int ans = 0;
for (auto u: all[c])
{
if (done2[u]) continue;
if (f[u] != u && !done2[f[u]])
{
ans++;
done2[u] = done2[f[u]] = 1;
}
}
return ans;
}
int main(void)
{
int n;
cin >> n;
map<string, int> mp;
int ind = 0;
for (int i = 1; i <= n; i++)
{
string s1, s2;
cin >> s1 >> s2;
if (mp.find(s1) == mp.end()) mp[s1] = ++ind;
if (mp.find(s2) == mp.end()) mp[s2] = ++ind;
f[mp[s1]] = mp[s2];
}
if (n&1)
{
cout << "-1\n";
return 0;
}
for (int i = 1; i <= n; i++)
if (!mark[i])
dfs(i);
for (int i = 1; i <= cc; i++)
{
for (int u = ini[i]; ; u = f[u])
{
incycle[u] = 1;
if (u == fim[i]) break;
}
}
int ans = n;
vector<int> v;
for (int i = 1; i <= n; i++)
{
if (!incycle[i] && !incycle[f[i]])
v.push_back(i);
else
all[comp[i]].push_back(i);
}
sort(v.begin(), v.end(), [&](int a, int b) {return nivel[a] > nivel[b];});
for (auto u: v)
{
if (done[u]) continue;
if (f[u] != u && !done[f[u]])
{
ans--;
done[u] = done[f[u]] = 1;
}
}
for (int c = 1; c <= cc; c++)
{
sort(all[c].begin(), all[c].end(), [&](int a, int b) {return nivel[a] > nivel[b];});
ans -= max(solve_ciclo(c), solve_nociclo(c));
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2796 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2668 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
318 ms |
15592 KB |
Output is correct |
5 |
Correct |
314 ms |
13420 KB |
Output is correct |
6 |
Correct |
316 ms |
15904 KB |
Output is correct |
7 |
Correct |
292 ms |
10860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
12904 KB |
Output is correct |
2 |
Correct |
312 ms |
13288 KB |
Output is correct |
3 |
Correct |
258 ms |
15852 KB |
Output is correct |
4 |
Correct |
283 ms |
10860 KB |
Output is correct |
5 |
Correct |
298 ms |
13928 KB |
Output is correct |
6 |
Correct |
298 ms |
12776 KB |
Output is correct |
7 |
Correct |
291 ms |
12932 KB |
Output is correct |
8 |
Correct |
286 ms |
14444 KB |
Output is correct |
9 |
Correct |
291 ms |
12776 KB |
Output is correct |
10 |
Correct |
233 ms |
12776 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2668 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2796 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
2 ms |
2668 KB |
Output is correct |
14 |
Correct |
2 ms |
2668 KB |
Output is correct |
15 |
Correct |
2 ms |
2668 KB |
Output is correct |
16 |
Correct |
2 ms |
2668 KB |
Output is correct |
17 |
Correct |
2 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
318 ms |
15592 KB |
Output is correct |
20 |
Correct |
314 ms |
13420 KB |
Output is correct |
21 |
Correct |
316 ms |
15904 KB |
Output is correct |
22 |
Correct |
292 ms |
10860 KB |
Output is correct |
23 |
Correct |
296 ms |
12904 KB |
Output is correct |
24 |
Correct |
312 ms |
13288 KB |
Output is correct |
25 |
Correct |
258 ms |
15852 KB |
Output is correct |
26 |
Correct |
283 ms |
10860 KB |
Output is correct |
27 |
Correct |
298 ms |
13928 KB |
Output is correct |
28 |
Correct |
298 ms |
12776 KB |
Output is correct |
29 |
Correct |
291 ms |
12932 KB |
Output is correct |
30 |
Correct |
286 ms |
14444 KB |
Output is correct |
31 |
Correct |
291 ms |
12776 KB |
Output is correct |
32 |
Correct |
233 ms |
12776 KB |
Output is correct |
33 |
Correct |
2 ms |
2668 KB |
Output is correct |
34 |
Correct |
2 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2668 KB |
Output is correct |
36 |
Correct |
2 ms |
2668 KB |
Output is correct |
37 |
Correct |
300 ms |
13028 KB |
Output is correct |
38 |
Correct |
314 ms |
13536 KB |
Output is correct |
39 |
Correct |
301 ms |
15464 KB |
Output is correct |
40 |
Correct |
306 ms |
15044 KB |
Output is correct |
41 |
Correct |
292 ms |
15076 KB |
Output is correct |
42 |
Correct |
300 ms |
15204 KB |
Output is correct |
43 |
Correct |
300 ms |
15336 KB |
Output is correct |
44 |
Correct |
302 ms |
15076 KB |
Output is correct |
45 |
Correct |
300 ms |
15076 KB |
Output is correct |
46 |
Correct |
306 ms |
15076 KB |
Output is correct |
47 |
Correct |
299 ms |
15024 KB |
Output is correct |
48 |
Correct |
302 ms |
15076 KB |
Output is correct |
49 |
Correct |
304 ms |
15464 KB |
Output is correct |
50 |
Correct |
269 ms |
17900 KB |
Output is correct |
51 |
Correct |
284 ms |
13292 KB |
Output is correct |
52 |
Correct |
327 ms |
16104 KB |
Output is correct |
53 |
Correct |
312 ms |
14824 KB |
Output is correct |
54 |
Correct |
293 ms |
14952 KB |
Output is correct |
55 |
Correct |
282 ms |
16492 KB |
Output is correct |
56 |
Correct |
290 ms |
14952 KB |
Output is correct |
57 |
Correct |
233 ms |
14952 KB |
Output is correct |
58 |
Correct |
2 ms |
2668 KB |
Output is correct |
59 |
Correct |
2 ms |
2668 KB |
Output is correct |
60 |
Correct |
2 ms |
2668 KB |
Output is correct |
61 |
Correct |
2 ms |
2668 KB |
Output is correct |
62 |
Correct |
2 ms |
2668 KB |
Output is correct |
63 |
Correct |
2 ms |
2668 KB |
Output is correct |
64 |
Correct |
2 ms |
2668 KB |
Output is correct |
65 |
Correct |
323 ms |
17768 KB |
Output is correct |
66 |
Correct |
309 ms |
15468 KB |
Output is correct |
67 |
Correct |
313 ms |
18024 KB |
Output is correct |
68 |
Correct |
288 ms |
13036 KB |
Output is correct |
69 |
Correct |
2 ms |
2668 KB |
Output is correct |
70 |
Correct |
2 ms |
2668 KB |
Output is correct |
71 |
Correct |
2 ms |
2668 KB |
Output is correct |
72 |
Correct |
2 ms |
2668 KB |
Output is correct |
73 |
Correct |
2 ms |
2668 KB |
Output is correct |
74 |
Correct |
2 ms |
2668 KB |
Output is correct |
75 |
Correct |
2 ms |
2668 KB |
Output is correct |
76 |
Correct |
2 ms |
2668 KB |
Output is correct |
77 |
Correct |
2 ms |
2668 KB |
Output is correct |
78 |
Correct |
2 ms |
2668 KB |
Output is correct |
79 |
Correct |
2 ms |
2668 KB |
Output is correct |
80 |
Correct |
2 ms |
2668 KB |
Output is correct |
81 |
Correct |
2 ms |
2668 KB |
Output is correct |
82 |
Correct |
2 ms |
2668 KB |
Output is correct |
83 |
Correct |
2 ms |
2668 KB |
Output is correct |