#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 5;
int n, ans;
map<string,int> mp;
set<int> s;
queue<int> q;
int deg[maxn], nxt[maxn], nxt_two[maxn], mark[maxn];
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("test.inp", "r", stdin);
// freopen("test.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++)
{
string u, v;
cin >> u >> v;
if (!mp.count(u)) mp[u] = mp.size() + 1;
if (!mp.count(v)) mp[v] = mp.size() + 1;
nxt[mp[u]] = mp[v];
deg[mp[v]]++;
}
if (mp.size() & 1) return cout << -1, 0;
n = mp.size();
for (int i = 1; i <= n; i++)
nxt_two[i] = nxt[nxt[i]],
s.emplace(i),
mark[i] = 1;
int cnt = n;
for (int i = 1; i <= n; i++)
if (mark[i] && nxt_two[i] == i && nxt[i] != i)
{
mark[i] = mark[nxt[i]] = 0;
s.erase(i); s.erase(nxt[i]);
cnt -= 2;
}
for (int i = 1; i <= n; i++)
if (mark[i] && deg[i] == 0)
q.emplace(i);
while (cnt)
{
if (q.empty()) q.emplace(*s.begin());
int u = q.front();
q.pop();
mark[u] = 0;
s.erase(u);
ans++;
cnt--;
if (mark[nxt[u]])
{
s.erase(nxt[u]);
cnt--;
mark[nxt[u]] = 0;
deg[nxt_two[u]]--;
if (mark[nxt_two[u]] && deg[nxt_two[u]] == 0)
q.emplace(nxt_two[u]);
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
183 ms |
15952 KB |
Output is correct |
5 |
Correct |
192 ms |
15956 KB |
Output is correct |
6 |
Correct |
181 ms |
15848 KB |
Output is correct |
7 |
Correct |
138 ms |
13140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
16180 KB |
Output is correct |
2 |
Correct |
169 ms |
16212 KB |
Output is correct |
3 |
Correct |
162 ms |
15952 KB |
Output is correct |
4 |
Correct |
142 ms |
13288 KB |
Output is correct |
5 |
Correct |
204 ms |
15952 KB |
Output is correct |
6 |
Correct |
169 ms |
16464 KB |
Output is correct |
7 |
Correct |
163 ms |
16468 KB |
Output is correct |
8 |
Correct |
157 ms |
16208 KB |
Output is correct |
9 |
Correct |
155 ms |
16496 KB |
Output is correct |
10 |
Correct |
125 ms |
15952 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
183 ms |
15952 KB |
Output is correct |
20 |
Correct |
192 ms |
15956 KB |
Output is correct |
21 |
Correct |
181 ms |
15848 KB |
Output is correct |
22 |
Correct |
138 ms |
13140 KB |
Output is correct |
23 |
Correct |
173 ms |
16180 KB |
Output is correct |
24 |
Correct |
169 ms |
16212 KB |
Output is correct |
25 |
Correct |
162 ms |
15952 KB |
Output is correct |
26 |
Correct |
142 ms |
13288 KB |
Output is correct |
27 |
Correct |
204 ms |
15952 KB |
Output is correct |
28 |
Correct |
169 ms |
16464 KB |
Output is correct |
29 |
Correct |
163 ms |
16468 KB |
Output is correct |
30 |
Correct |
157 ms |
16208 KB |
Output is correct |
31 |
Correct |
155 ms |
16496 KB |
Output is correct |
32 |
Correct |
125 ms |
15952 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
0 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
189 ms |
15956 KB |
Output is correct |
38 |
Correct |
184 ms |
16464 KB |
Output is correct |
39 |
Correct |
177 ms |
16464 KB |
Output is correct |
40 |
Correct |
169 ms |
16464 KB |
Output is correct |
41 |
Correct |
182 ms |
16468 KB |
Output is correct |
42 |
Correct |
183 ms |
16212 KB |
Output is correct |
43 |
Correct |
203 ms |
16196 KB |
Output is correct |
44 |
Correct |
182 ms |
16212 KB |
Output is correct |
45 |
Correct |
211 ms |
16212 KB |
Output is correct |
46 |
Correct |
181 ms |
16208 KB |
Output is correct |
47 |
Correct |
187 ms |
16468 KB |
Output is correct |
48 |
Correct |
192 ms |
16260 KB |
Output is correct |
49 |
Correct |
185 ms |
16212 KB |
Output is correct |
50 |
Correct |
150 ms |
15956 KB |
Output is correct |
51 |
Correct |
147 ms |
13176 KB |
Output is correct |
52 |
Correct |
182 ms |
15892 KB |
Output is correct |
53 |
Correct |
174 ms |
16336 KB |
Output is correct |
54 |
Correct |
179 ms |
16464 KB |
Output is correct |
55 |
Correct |
167 ms |
16400 KB |
Output is correct |
56 |
Correct |
163 ms |
16464 KB |
Output is correct |
57 |
Correct |
119 ms |
15956 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
0 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
0 ms |
2396 KB |
Output is correct |
62 |
Correct |
0 ms |
2396 KB |
Output is correct |
63 |
Correct |
1 ms |
2396 KB |
Output is correct |
64 |
Correct |
1 ms |
2396 KB |
Output is correct |
65 |
Correct |
183 ms |
15896 KB |
Output is correct |
66 |
Correct |
180 ms |
15912 KB |
Output is correct |
67 |
Correct |
187 ms |
16040 KB |
Output is correct |
68 |
Correct |
144 ms |
13332 KB |
Output is correct |
69 |
Correct |
1 ms |
2392 KB |
Output is correct |
70 |
Correct |
1 ms |
2396 KB |
Output is correct |
71 |
Correct |
1 ms |
2396 KB |
Output is correct |
72 |
Correct |
0 ms |
2396 KB |
Output is correct |
73 |
Correct |
0 ms |
2396 KB |
Output is correct |
74 |
Correct |
0 ms |
2396 KB |
Output is correct |
75 |
Correct |
0 ms |
2396 KB |
Output is correct |
76 |
Correct |
0 ms |
2396 KB |
Output is correct |
77 |
Correct |
0 ms |
2396 KB |
Output is correct |
78 |
Correct |
0 ms |
2396 KB |
Output is correct |
79 |
Correct |
1 ms |
2396 KB |
Output is correct |
80 |
Correct |
1 ms |
2392 KB |
Output is correct |
81 |
Correct |
0 ms |
2396 KB |
Output is correct |
82 |
Correct |
0 ms |
2396 KB |
Output is correct |
83 |
Correct |
1 ms |
2392 KB |
Output is correct |