#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int N, idx, pre[100005], vis[100005], visr[100005], deg[100005], ans;
bool used[100005];
map<string, int> mp;
vector<int> E[100005], rE[100005], newE[100005];
vector<pii> oE;
void dfs(int k, int r)
{
for (int i : E[k])
if (!vis[i] || (visr[i] == r && vis[i] == 1))
{
vis[i]++;
visr[i] = r;
dfs(i, r);
}
}
void tree_matching(int k)
{
for (int i : rE[k])
if (vis[i] != 2)
tree_matching(i);
for (int i : rE[k])
if (vis[i] != 2 && !used[i])
{
used[k] = used[i] = 1;
ans++;
break;
}
}
void tree_matching2(int k)
{
vis[k] = 3;
for (int i : newE[k])
if (vis[i] != 3 && !used[i])
tree_matching2(i);
for (int i : newE[k])
if (!used[i])
{
used[k] = used[i] = 1;
ans++;
break;
}
vis[k] = 0;
}
#define debug(arr) \
for (int i = 1; i <= N; i++) \
cout << arr[i] << " \n"[i == N];
signed main()
{
cin >> N;
if (N % 2 == 1)
{
cout << -1 << '\n';
return 0;
}
for (int i = 1; i <= N; i++)
{
string s, t;
cin >> s >> t;
if (mp[s] == 0)
mp[s] = ++idx;
if (mp[t] == 0)
mp[t] = ++idx;
oE.emplace_back(make_pair(mp[s], mp[t]));
}
//for (auto p : mp)
// cout << p.F << " " << p.S << '\n';
sort(oE.begin(), oE.end(), [](pii p1, pii p2)
{ return pii{min(p1.F, p1.S), max(p1.F, p1.S)} < pii{min(p2.F, p2.S), max(p2.F, p2.S)}; });
for (int i = 1; i < N; i++)
if (oE[i].F == oE[i - 1].S && oE[i].S == oE[i - 1].F)
used[oE[i].F] = used[oE[i].S] = 1;
//cout << "pair ";
//debug(used);
for (pii p : oE)
if (!used[p.F])
{
int u = p.F, v = (used[p.S] ? p.F : p.S);
E[u].emplace_back(v);
rE[v].emplace_back(u);
}
mp.clear();
for (int i = 1; i <= N; i++)
if (!vis[i])
{
vis[i] = 1;
visr[i] = i;
dfs(i, i);
}
//cout << "vis ";
//debug(vis);
for (int i = 1; i <= N; i++)
if (vis[i] == 2)
{
tree_matching(i);
//cout << "firstTree ";
//debug(used);
}
for (int i = 1; i <= N; i++)
if (!used[i])
for (int j : E[i])
if (!used[j])
newE[i].emplace_back(j);
for (int i = 1; i <= N; i++)
if (!used[i])
tree_matching2(i);
//cout << "secondChain ";
//debug(used);
for (int i = 1; i <= N; i++)
if (!used[i])
ans++;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
4 ms |
7344 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
3 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
4 ms |
7244 KB |
Output is correct |
10 |
Correct |
4 ms |
7356 KB |
Output is correct |
11 |
Correct |
3 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7356 KB |
Output is correct |
14 |
Correct |
5 ms |
7352 KB |
Output is correct |
15 |
Correct |
4 ms |
7248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Correct |
4 ms |
7244 KB |
Output is correct |
4 |
Correct |
296 ms |
31300 KB |
Output is correct |
5 |
Correct |
290 ms |
22736 KB |
Output is correct |
6 |
Correct |
318 ms |
32576 KB |
Output is correct |
7 |
Correct |
4 ms |
7244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
313 ms |
22268 KB |
Output is correct |
2 |
Correct |
310 ms |
23576 KB |
Output is correct |
3 |
Correct |
240 ms |
23044 KB |
Output is correct |
4 |
Correct |
4 ms |
7244 KB |
Output is correct |
5 |
Correct |
365 ms |
27320 KB |
Output is correct |
6 |
Correct |
310 ms |
21564 KB |
Output is correct |
7 |
Correct |
279 ms |
21460 KB |
Output is correct |
8 |
Correct |
273 ms |
22004 KB |
Output is correct |
9 |
Correct |
291 ms |
21148 KB |
Output is correct |
10 |
Correct |
204 ms |
20196 KB |
Output is correct |
11 |
Correct |
4 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7244 KB |
Output is correct |
14 |
Correct |
4 ms |
7308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
4 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
4 ms |
7344 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
3 ms |
7244 KB |
Output is correct |
8 |
Correct |
4 ms |
7244 KB |
Output is correct |
9 |
Correct |
4 ms |
7244 KB |
Output is correct |
10 |
Correct |
4 ms |
7356 KB |
Output is correct |
11 |
Correct |
3 ms |
7244 KB |
Output is correct |
12 |
Correct |
4 ms |
7244 KB |
Output is correct |
13 |
Correct |
4 ms |
7356 KB |
Output is correct |
14 |
Correct |
5 ms |
7352 KB |
Output is correct |
15 |
Correct |
4 ms |
7248 KB |
Output is correct |
16 |
Correct |
5 ms |
7244 KB |
Output is correct |
17 |
Correct |
4 ms |
7244 KB |
Output is correct |
18 |
Correct |
4 ms |
7244 KB |
Output is correct |
19 |
Correct |
296 ms |
31300 KB |
Output is correct |
20 |
Correct |
290 ms |
22736 KB |
Output is correct |
21 |
Correct |
318 ms |
32576 KB |
Output is correct |
22 |
Correct |
4 ms |
7244 KB |
Output is correct |
23 |
Correct |
313 ms |
22268 KB |
Output is correct |
24 |
Correct |
310 ms |
23576 KB |
Output is correct |
25 |
Correct |
240 ms |
23044 KB |
Output is correct |
26 |
Correct |
4 ms |
7244 KB |
Output is correct |
27 |
Correct |
365 ms |
27320 KB |
Output is correct |
28 |
Correct |
310 ms |
21564 KB |
Output is correct |
29 |
Correct |
279 ms |
21460 KB |
Output is correct |
30 |
Correct |
273 ms |
22004 KB |
Output is correct |
31 |
Correct |
291 ms |
21148 KB |
Output is correct |
32 |
Correct |
204 ms |
20196 KB |
Output is correct |
33 |
Correct |
4 ms |
7244 KB |
Output is correct |
34 |
Correct |
4 ms |
7244 KB |
Output is correct |
35 |
Correct |
4 ms |
7244 KB |
Output is correct |
36 |
Correct |
4 ms |
7308 KB |
Output is correct |
37 |
Correct |
332 ms |
24904 KB |
Output is correct |
38 |
Correct |
280 ms |
25348 KB |
Output is correct |
39 |
Correct |
265 ms |
23976 KB |
Output is correct |
40 |
Correct |
278 ms |
23616 KB |
Output is correct |
41 |
Correct |
326 ms |
23636 KB |
Output is correct |
42 |
Correct |
291 ms |
24096 KB |
Output is correct |
43 |
Correct |
305 ms |
24188 KB |
Output is correct |
44 |
Correct |
264 ms |
24064 KB |
Output is correct |
45 |
Correct |
324 ms |
24128 KB |
Output is correct |
46 |
Correct |
271 ms |
24232 KB |
Output is correct |
47 |
Correct |
251 ms |
23340 KB |
Output is correct |
48 |
Correct |
304 ms |
24400 KB |
Output is correct |
49 |
Correct |
289 ms |
25772 KB |
Output is correct |
50 |
Correct |
274 ms |
25248 KB |
Output is correct |
51 |
Correct |
4 ms |
7364 KB |
Output is correct |
52 |
Correct |
266 ms |
29468 KB |
Output is correct |
53 |
Correct |
258 ms |
23788 KB |
Output is correct |
54 |
Correct |
283 ms |
23640 KB |
Output is correct |
55 |
Correct |
254 ms |
24100 KB |
Output is correct |
56 |
Correct |
272 ms |
23136 KB |
Output is correct |
57 |
Correct |
200 ms |
22320 KB |
Output is correct |
58 |
Correct |
4 ms |
7340 KB |
Output is correct |
59 |
Correct |
4 ms |
7348 KB |
Output is correct |
60 |
Correct |
4 ms |
7248 KB |
Output is correct |
61 |
Correct |
4 ms |
7344 KB |
Output is correct |
62 |
Correct |
3 ms |
7348 KB |
Output is correct |
63 |
Correct |
4 ms |
7248 KB |
Output is correct |
64 |
Correct |
4 ms |
7336 KB |
Output is correct |
65 |
Correct |
291 ms |
33416 KB |
Output is correct |
66 |
Correct |
307 ms |
24960 KB |
Output is correct |
67 |
Correct |
338 ms |
34648 KB |
Output is correct |
68 |
Correct |
4 ms |
7376 KB |
Output is correct |
69 |
Correct |
4 ms |
7368 KB |
Output is correct |
70 |
Correct |
4 ms |
7376 KB |
Output is correct |
71 |
Correct |
3 ms |
7376 KB |
Output is correct |
72 |
Correct |
4 ms |
7352 KB |
Output is correct |
73 |
Correct |
3 ms |
7248 KB |
Output is correct |
74 |
Correct |
4 ms |
7248 KB |
Output is correct |
75 |
Correct |
4 ms |
7332 KB |
Output is correct |
76 |
Correct |
4 ms |
7352 KB |
Output is correct |
77 |
Correct |
3 ms |
7248 KB |
Output is correct |
78 |
Correct |
4 ms |
7352 KB |
Output is correct |
79 |
Correct |
5 ms |
7352 KB |
Output is correct |
80 |
Correct |
5 ms |
7332 KB |
Output is correct |
81 |
Correct |
4 ms |
7248 KB |
Output is correct |
82 |
Correct |
5 ms |
7248 KB |
Output is correct |
83 |
Correct |
4 ms |
7344 KB |
Output is correct |