#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
if (n%2)
{
cout << "-1" << endl;
return 0;
}
map<string, int> m;
vector<int> il(n, -1), lm(n, 0), happy(n, 0); // koho ja lubim, kolko postav lubi mna?
int cnt = 0;
for (int i = 0; i < n; i++)
{
string a, b;
cin >> a >> b;
if (!m.count(a)) m[a] = cnt++;
if (!m.count(b)) m[b] = cnt++;
int ia = m[a], ib = m[b];
il[ia] = ib, lm[ib]++;
if (ia != ib && il[ib] == ia) happy[ia] = happy[ib] = true;
}
vector<int> stk;
for (int i = 0; i < n; i++)
if (lm[i] == 0) stk.push_back(i);
int ans = 0, alone = 0;
while (!stk.empty())
{
int i = stk.back(); stk.pop_back();
if (happy[il[i]])
{
alone++;
happy[i] = true;
continue;
}
happy[i] = happy[il[i]] = true;
ans++;
if (!happy[il[il[i]]])
{
lm[il[il[i]]]--;
if (!lm[il[il[i]]]) stk.push_back(il[il[i]]);
}
}
for (int i = 0; i < n; i++)
{
if (happy[i]) continue;
int j = il[i], len = 1;
happy[i] = true;
while (j != i)
{
happy[j] = true;
len++;
j = il[j];
}
ans += (len>>1), alone += (len&1);
}
cout << ans+alone << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
210 ms |
11452 KB |
Output is correct |
5 |
Correct |
218 ms |
11460 KB |
Output is correct |
6 |
Correct |
213 ms |
11568 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
221 ms |
9428 KB |
Output is correct |
2 |
Correct |
212 ms |
9400 KB |
Output is correct |
3 |
Correct |
170 ms |
9204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
205 ms |
9276 KB |
Output is correct |
6 |
Correct |
210 ms |
9624 KB |
Output is correct |
7 |
Correct |
210 ms |
9708 KB |
Output is correct |
8 |
Correct |
189 ms |
9580 KB |
Output is correct |
9 |
Correct |
192 ms |
9928 KB |
Output is correct |
10 |
Correct |
157 ms |
9836 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
2 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
210 ms |
11452 KB |
Output is correct |
20 |
Correct |
218 ms |
11460 KB |
Output is correct |
21 |
Correct |
213 ms |
11568 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
221 ms |
9428 KB |
Output is correct |
24 |
Correct |
212 ms |
9400 KB |
Output is correct |
25 |
Correct |
170 ms |
9204 KB |
Output is correct |
26 |
Correct |
0 ms |
204 KB |
Output is correct |
27 |
Correct |
205 ms |
9276 KB |
Output is correct |
28 |
Correct |
210 ms |
9624 KB |
Output is correct |
29 |
Correct |
210 ms |
9708 KB |
Output is correct |
30 |
Correct |
189 ms |
9580 KB |
Output is correct |
31 |
Correct |
192 ms |
9928 KB |
Output is correct |
32 |
Correct |
157 ms |
9836 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
215 ms |
11612 KB |
Output is correct |
38 |
Correct |
217 ms |
11776 KB |
Output is correct |
39 |
Correct |
204 ms |
11936 KB |
Output is correct |
40 |
Correct |
214 ms |
11856 KB |
Output is correct |
41 |
Correct |
207 ms |
11736 KB |
Output is correct |
42 |
Correct |
208 ms |
11844 KB |
Output is correct |
43 |
Correct |
212 ms |
11740 KB |
Output is correct |
44 |
Correct |
212 ms |
11844 KB |
Output is correct |
45 |
Correct |
205 ms |
11824 KB |
Output is correct |
46 |
Correct |
211 ms |
11852 KB |
Output is correct |
47 |
Correct |
198 ms |
12108 KB |
Output is correct |
48 |
Correct |
207 ms |
11588 KB |
Output is correct |
49 |
Correct |
224 ms |
11488 KB |
Output is correct |
50 |
Correct |
173 ms |
11460 KB |
Output is correct |
51 |
Correct |
0 ms |
332 KB |
Output is correct |
52 |
Correct |
211 ms |
11440 KB |
Output is correct |
53 |
Correct |
209 ms |
11832 KB |
Output is correct |
54 |
Correct |
207 ms |
11816 KB |
Output is correct |
55 |
Correct |
196 ms |
11972 KB |
Output is correct |
56 |
Correct |
192 ms |
12052 KB |
Output is correct |
57 |
Correct |
156 ms |
12060 KB |
Output is correct |
58 |
Correct |
0 ms |
204 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
204 KB |
Output is correct |
61 |
Correct |
0 ms |
204 KB |
Output is correct |
62 |
Correct |
0 ms |
204 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
213 ms |
11412 KB |
Output is correct |
66 |
Correct |
209 ms |
11368 KB |
Output is correct |
67 |
Correct |
204 ms |
11460 KB |
Output is correct |
68 |
Correct |
0 ms |
332 KB |
Output is correct |
69 |
Correct |
0 ms |
204 KB |
Output is correct |
70 |
Correct |
1 ms |
204 KB |
Output is correct |
71 |
Correct |
1 ms |
308 KB |
Output is correct |
72 |
Correct |
0 ms |
204 KB |
Output is correct |
73 |
Correct |
0 ms |
204 KB |
Output is correct |
74 |
Correct |
1 ms |
312 KB |
Output is correct |
75 |
Correct |
0 ms |
204 KB |
Output is correct |
76 |
Correct |
0 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
0 ms |
204 KB |
Output is correct |
79 |
Correct |
0 ms |
204 KB |
Output is correct |
80 |
Correct |
0 ms |
204 KB |
Output is correct |
81 |
Correct |
0 ms |
204 KB |
Output is correct |
82 |
Correct |
1 ms |
312 KB |
Output is correct |
83 |
Correct |
0 ms |
204 KB |
Output is correct |