#include <bits/stdc++.h>
using namespace std;
int n;
int bruvv;
map<string, int> nume;
int f[100005];
int indeg[100005];
bool taken[100005];
bool viz[100005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++)
{
string x, y;
cin >> x >> y;
if (nume[x] == 0)
nume[x] = ++bruvv;
if (nume[y] == 0)
nume[y] = ++bruvv;
int xx = nume[x], yy = nume[y];
f[xx] = yy;
}
if (n % 2 == 1)
{
cout << -1;
return 0;
}
set<pair<int,int>> s;
int op = n;
for (int i = 1; i <= n; i++)
{
if (f[i] != i and f[f[i]] == i)
op--, taken[i] = true;
else
indeg[f[i]]++;
}
for (int i = 1; i <= n; i++)
{
if (f[i] == i or f[f[i]] != i)
s.insert({indeg[i], i});
}
bool wow = false;
while (!s.empty())
{
pair<int,int> pr = *s.begin();
s.erase(pr);
int x = pr.second;
if (indeg[x] != 0)
{
wow = true;
break;
}
if (!taken[f[x]])
{
taken[x] = true;
taken[f[x]] = true;
op--;
s.erase({indeg[f[x]], f[x]});
if (!taken[f[f[x]]])
{
s.erase({indeg[f[f[x]]], f[f[x]]});
indeg[f[f[x]]]--;
s.insert({indeg[f[f[x]]], f[f[x]]});
}
}
else
f[x] = x;
}
if (wow)
{
vector<int> noduri_lmao;
for (int i = 1; i <= n; i++)
if (!taken[i])
noduri_lmao.push_back(i);
for (auto it : noduri_lmao)
{
if (!viz[it])
{
int sz = 0;
int xx = it;
while (!viz[xx])
{
viz[xx] = true;
sz++;
xx = f[xx];
}
op -= (sz - (sz / 2 + sz % 2));
}
}
}
cout << op;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
416 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
142 ms |
16440 KB |
Output is correct |
5 |
Correct |
165 ms |
16212 KB |
Output is correct |
6 |
Correct |
149 ms |
16332 KB |
Output is correct |
7 |
Correct |
134 ms |
10576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
16092 KB |
Output is correct |
2 |
Correct |
173 ms |
15960 KB |
Output is correct |
3 |
Correct |
130 ms |
16332 KB |
Output is correct |
4 |
Correct |
131 ms |
10576 KB |
Output is correct |
5 |
Correct |
171 ms |
15952 KB |
Output is correct |
6 |
Correct |
166 ms |
15952 KB |
Output is correct |
7 |
Correct |
163 ms |
16048 KB |
Output is correct |
8 |
Correct |
148 ms |
16336 KB |
Output is correct |
9 |
Correct |
141 ms |
15952 KB |
Output is correct |
10 |
Correct |
118 ms |
15360 KB |
Output is correct |
11 |
Correct |
1 ms |
344 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 |
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 |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
416 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
352 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 |
344 KB |
Output is correct |
19 |
Correct |
142 ms |
16440 KB |
Output is correct |
20 |
Correct |
165 ms |
16212 KB |
Output is correct |
21 |
Correct |
149 ms |
16332 KB |
Output is correct |
22 |
Correct |
134 ms |
10576 KB |
Output is correct |
23 |
Correct |
192 ms |
16092 KB |
Output is correct |
24 |
Correct |
173 ms |
15960 KB |
Output is correct |
25 |
Correct |
130 ms |
16332 KB |
Output is correct |
26 |
Correct |
131 ms |
10576 KB |
Output is correct |
27 |
Correct |
171 ms |
15952 KB |
Output is correct |
28 |
Correct |
166 ms |
15952 KB |
Output is correct |
29 |
Correct |
163 ms |
16048 KB |
Output is correct |
30 |
Correct |
148 ms |
16336 KB |
Output is correct |
31 |
Correct |
141 ms |
15952 KB |
Output is correct |
32 |
Correct |
118 ms |
15360 KB |
Output is correct |
33 |
Correct |
1 ms |
344 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 |
180 ms |
15956 KB |
Output is correct |
38 |
Correct |
170 ms |
15952 KB |
Output is correct |
39 |
Correct |
165 ms |
15932 KB |
Output is correct |
40 |
Correct |
164 ms |
15956 KB |
Output is correct |
41 |
Correct |
166 ms |
15952 KB |
Output is correct |
42 |
Correct |
186 ms |
16088 KB |
Output is correct |
43 |
Correct |
173 ms |
16208 KB |
Output is correct |
44 |
Correct |
173 ms |
15972 KB |
Output is correct |
45 |
Correct |
163 ms |
15860 KB |
Output is correct |
46 |
Correct |
172 ms |
15956 KB |
Output is correct |
47 |
Correct |
142 ms |
15952 KB |
Output is correct |
48 |
Correct |
170 ms |
15952 KB |
Output is correct |
49 |
Correct |
174 ms |
15952 KB |
Output is correct |
50 |
Correct |
129 ms |
16504 KB |
Output is correct |
51 |
Correct |
126 ms |
10576 KB |
Output is correct |
52 |
Correct |
166 ms |
15956 KB |
Output is correct |
53 |
Correct |
167 ms |
15764 KB |
Output is correct |
54 |
Correct |
162 ms |
15956 KB |
Output is correct |
55 |
Correct |
143 ms |
16336 KB |
Output is correct |
56 |
Correct |
141 ms |
15956 KB |
Output is correct |
57 |
Correct |
116 ms |
15464 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 |
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 |
142 ms |
16532 KB |
Output is correct |
66 |
Correct |
147 ms |
16384 KB |
Output is correct |
67 |
Correct |
143 ms |
16484 KB |
Output is correct |
68 |
Correct |
127 ms |
10584 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
76 |
Correct |
1 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 |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |
81 |
Correct |
1 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |