#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef pair <int,pii> piii;
#define forr(_a,_b,_c) for(int _a = (_b); _a <= int (_c); ++_a)
#define ford(_a,_b,_c) for(int _a = (_b) + 1; _a --> int (_c);)
#define forf(_a,_b,_c) for(int _a = (_b); _a < int (_c); ++_a)
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
template<class X, class Y>
bool minz(X &x, const Y &y) {
if (x > y) {
x = y;
return true;
} return false;
}
template<class X, class Y>
bool maxz(X &x, const Y &y) {
if (x < y) {
x = y;
return true;
} return false;
}
const int N = 5e5 + 5;
const ll oo = (ll) 1e16;
const ll mod = 1e9 + 7; // 998244353;
int vis[N], edge[N], dp[N][4], ban[N], n, rem[N];
vector <int> a[N], ra[N];
vector <pair<pii, pii>> s;
string u, v;
map <string, int> id;
void dfs (int u, int p){
vis[u] = 1;
for (int i : a[u]){
int v = edge[i] ^ u;
if (rem[v]) continue;
if (v != p && vis[v] == 0){
dfs(v, u);
} else if (vis[v] == 1){
pair<pii, pii> ww;
ww.st = mp(u, i);
for (int j : a[v]){
int w = edge[j] ^ u;
ww.nd = mp(v, j);
break;
}
s.pb(ww);
}
}
vis[u] = 2;
}
void go (int u){
dp[u][0] = dp[u][1] = 0;
for (int i : ra[u]){
int v = edge[i] ^ u;
if (ban[i] || rem[v]) continue;
go(v);
dp[u][1] = max (dp[u][1] + dp[v][1], dp[u][0] + dp[v][0] + 1);
dp[u][0] += dp[v][1];
}
dp[u][1] = max (dp[u][0], dp[u][1]);
}
int cnt;
map <pii, int> c2;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef hqm
freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);
#endif
cin >> n;
if (n & 1){
cout << "-1\n";
return 0;
}
int res = 0;
forr (i, 1, n){
cin >> u >> v;
if (!id.count(u))
id[u] = ++cnt;
if (!id.count(v))
id[v] = ++cnt;
a[id[u]].pb(i);
ra[id[v]].pb(i);
edge[i] = id[u] ^ id[v];
c2[mp(id[u], id[v])] = 1;
if (c2.count(mp(id[v], id[u])) && u != v){
rem[id[u]] = rem[id[v]] = 1;
//cout << u << " " << v << endl;
res += 2;
}
}
forr (i, 1, n)
if (rem[i]){
for (int j : ra[i])
edge[j] = 0;
}
forr (i, 1, n)
if (!vis[i] && !rem[i])
dfs(i, -1);
for (pair<pii, pii> e : s){
pii u = e.st, v = e.nd;
//cout << u.st << " " << (edge[u.nd] ^ u.st) << " " << v.st << " " << (edge[v.nd] ^ v.st) << endl;
ban[u.nd] = 1;
go(u.st);
int ans = dp[u.st][1];
ban[u.nd] = 0;
ban[v.nd] = 1;
go(v.st);
maxz(ans, dp[v.st][1]);
ban[v.nd] = 0;
res += ans;
}
cout << n - res << "\n";
return 0;
}
/*
*/
Compilation message
polygon.cpp: In function 'void dfs(int, int)':
polygon.cpp:59:21: warning: unused variable 'w' [-Wunused-variable]
59 | int w = edge[j] ^ u;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23948 KB |
Output is correct |
3 |
Correct |
10 ms |
23984 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
13 ms |
23900 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23956 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23896 KB |
Output is correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
12 |
Correct |
10 ms |
23900 KB |
Output is correct |
13 |
Correct |
10 ms |
23900 KB |
Output is correct |
14 |
Correct |
10 ms |
23900 KB |
Output is correct |
15 |
Correct |
10 ms |
23880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
23896 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
10 ms |
23900 KB |
Output is correct |
4 |
Correct |
377 ms |
58212 KB |
Output is correct |
5 |
Correct |
343 ms |
49872 KB |
Output is correct |
6 |
Correct |
383 ms |
59540 KB |
Output is correct |
7 |
Correct |
10 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
48460 KB |
Output is correct |
2 |
Correct |
367 ms |
50156 KB |
Output is correct |
3 |
Correct |
270 ms |
50888 KB |
Output is correct |
4 |
Correct |
10 ms |
23896 KB |
Output is correct |
5 |
Correct |
424 ms |
55840 KB |
Output is correct |
6 |
Correct |
332 ms |
47184 KB |
Output is correct |
7 |
Correct |
369 ms |
47696 KB |
Output is correct |
8 |
Correct |
304 ms |
48792 KB |
Output is correct |
9 |
Correct |
300 ms |
46672 KB |
Output is correct |
10 |
Correct |
212 ms |
46024 KB |
Output is correct |
11 |
Correct |
10 ms |
23896 KB |
Output is correct |
12 |
Correct |
10 ms |
23952 KB |
Output is correct |
13 |
Correct |
10 ms |
23900 KB |
Output is correct |
14 |
Correct |
10 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23948 KB |
Output is correct |
3 |
Correct |
10 ms |
23984 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
13 ms |
23900 KB |
Output is correct |
6 |
Correct |
11 ms |
23900 KB |
Output is correct |
7 |
Correct |
11 ms |
23956 KB |
Output is correct |
8 |
Correct |
10 ms |
23896 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
10 ms |
23896 KB |
Output is correct |
11 |
Correct |
10 ms |
23900 KB |
Output is correct |
12 |
Correct |
10 ms |
23900 KB |
Output is correct |
13 |
Correct |
10 ms |
23900 KB |
Output is correct |
14 |
Correct |
10 ms |
23900 KB |
Output is correct |
15 |
Correct |
10 ms |
23880 KB |
Output is correct |
16 |
Correct |
9 ms |
23896 KB |
Output is correct |
17 |
Correct |
10 ms |
23900 KB |
Output is correct |
18 |
Correct |
10 ms |
23900 KB |
Output is correct |
19 |
Correct |
377 ms |
58212 KB |
Output is correct |
20 |
Correct |
343 ms |
49872 KB |
Output is correct |
21 |
Correct |
383 ms |
59540 KB |
Output is correct |
22 |
Correct |
10 ms |
23900 KB |
Output is correct |
23 |
Correct |
371 ms |
48460 KB |
Output is correct |
24 |
Correct |
367 ms |
50156 KB |
Output is correct |
25 |
Correct |
270 ms |
50888 KB |
Output is correct |
26 |
Correct |
10 ms |
23896 KB |
Output is correct |
27 |
Correct |
424 ms |
55840 KB |
Output is correct |
28 |
Correct |
332 ms |
47184 KB |
Output is correct |
29 |
Correct |
369 ms |
47696 KB |
Output is correct |
30 |
Correct |
304 ms |
48792 KB |
Output is correct |
31 |
Correct |
300 ms |
46672 KB |
Output is correct |
32 |
Correct |
212 ms |
46024 KB |
Output is correct |
33 |
Correct |
10 ms |
23896 KB |
Output is correct |
34 |
Correct |
10 ms |
23952 KB |
Output is correct |
35 |
Correct |
10 ms |
23900 KB |
Output is correct |
36 |
Correct |
10 ms |
23900 KB |
Output is correct |
37 |
Correct |
375 ms |
48468 KB |
Output is correct |
38 |
Correct |
335 ms |
49100 KB |
Output is correct |
39 |
Correct |
377 ms |
48388 KB |
Output is correct |
40 |
Correct |
342 ms |
47100 KB |
Output is correct |
41 |
Correct |
340 ms |
47188 KB |
Output is correct |
42 |
Correct |
336 ms |
47696 KB |
Output is correct |
43 |
Correct |
387 ms |
47704 KB |
Output is correct |
44 |
Correct |
367 ms |
47560 KB |
Output is correct |
45 |
Correct |
356 ms |
47600 KB |
Output is correct |
46 |
Correct |
368 ms |
47696 KB |
Output is correct |
47 |
Correct |
326 ms |
46972 KB |
Output is correct |
48 |
Correct |
366 ms |
48464 KB |
Output is correct |
49 |
Correct |
385 ms |
50000 KB |
Output is correct |
50 |
Correct |
273 ms |
50808 KB |
Output is correct |
51 |
Correct |
10 ms |
23896 KB |
Output is correct |
52 |
Correct |
379 ms |
55880 KB |
Output is correct |
53 |
Correct |
339 ms |
47304 KB |
Output is correct |
54 |
Correct |
357 ms |
47696 KB |
Output is correct |
55 |
Correct |
299 ms |
48840 KB |
Output is correct |
56 |
Correct |
302 ms |
46928 KB |
Output is correct |
57 |
Correct |
208 ms |
45872 KB |
Output is correct |
58 |
Correct |
10 ms |
23896 KB |
Output is correct |
59 |
Correct |
10 ms |
23896 KB |
Output is correct |
60 |
Correct |
10 ms |
23900 KB |
Output is correct |
61 |
Correct |
11 ms |
23864 KB |
Output is correct |
62 |
Correct |
12 ms |
23896 KB |
Output is correct |
63 |
Correct |
10 ms |
23900 KB |
Output is correct |
64 |
Correct |
10 ms |
23820 KB |
Output is correct |
65 |
Correct |
398 ms |
58344 KB |
Output is correct |
66 |
Correct |
377 ms |
49940 KB |
Output is correct |
67 |
Correct |
410 ms |
59740 KB |
Output is correct |
68 |
Correct |
10 ms |
23896 KB |
Output is correct |
69 |
Correct |
10 ms |
23900 KB |
Output is correct |
70 |
Correct |
10 ms |
23900 KB |
Output is correct |
71 |
Correct |
13 ms |
23936 KB |
Output is correct |
72 |
Correct |
10 ms |
23900 KB |
Output is correct |
73 |
Correct |
10 ms |
23776 KB |
Output is correct |
74 |
Correct |
10 ms |
23900 KB |
Output is correct |
75 |
Correct |
12 ms |
23896 KB |
Output is correct |
76 |
Correct |
9 ms |
23932 KB |
Output is correct |
77 |
Correct |
10 ms |
23824 KB |
Output is correct |
78 |
Correct |
10 ms |
23900 KB |
Output is correct |
79 |
Correct |
10 ms |
23736 KB |
Output is correct |
80 |
Correct |
11 ms |
23900 KB |
Output is correct |
81 |
Correct |
11 ms |
23900 KB |
Output is correct |
82 |
Correct |
11 ms |
23812 KB |
Output is correct |
83 |
Correct |
14 ms |
23896 KB |
Output is correct |