#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;
if (vis[w] == 1){
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;
}
}
for (pair <string, int> w : id){
// cout << w.st << " " << w.nd << endl;
}
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;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23932 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23928 KB |
Output is correct |
6 |
Correct |
10 ms |
23928 KB |
Output is correct |
7 |
Correct |
10 ms |
23900 KB |
Output is correct |
8 |
Correct |
15 ms |
23900 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23824 KB |
Output is correct |
11 |
Correct |
10 ms |
23896 KB |
Output is correct |
12 |
Correct |
10 ms |
23856 KB |
Output is correct |
13 |
Correct |
10 ms |
23844 KB |
Output is correct |
14 |
Correct |
11 ms |
23976 KB |
Output is correct |
15 |
Correct |
12 ms |
23896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23896 KB |
Output is correct |
3 |
Correct |
11 ms |
23900 KB |
Output is correct |
4 |
Correct |
410 ms |
58528 KB |
Output is correct |
5 |
Correct |
357 ms |
49876 KB |
Output is correct |
6 |
Correct |
398 ms |
59796 KB |
Output is correct |
7 |
Correct |
11 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
386 ms |
48308 KB |
Output is correct |
2 |
Correct |
387 ms |
50000 KB |
Output is correct |
3 |
Correct |
285 ms |
50892 KB |
Output is correct |
4 |
Correct |
11 ms |
23900 KB |
Output is correct |
5 |
Correct |
374 ms |
56104 KB |
Output is correct |
6 |
Correct |
356 ms |
47200 KB |
Output is correct |
7 |
Correct |
355 ms |
47624 KB |
Output is correct |
8 |
Correct |
315 ms |
48844 KB |
Output is correct |
9 |
Correct |
323 ms |
46876 KB |
Output is correct |
10 |
Correct |
221 ms |
46088 KB |
Output is correct |
11 |
Correct |
10 ms |
23896 KB |
Output is correct |
12 |
Correct |
10 ms |
23900 KB |
Output is correct |
13 |
Correct |
11 ms |
23900 KB |
Output is correct |
14 |
Correct |
10 ms |
23940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23932 KB |
Output is correct |
4 |
Correct |
10 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
23928 KB |
Output is correct |
6 |
Correct |
10 ms |
23928 KB |
Output is correct |
7 |
Correct |
10 ms |
23900 KB |
Output is correct |
8 |
Correct |
15 ms |
23900 KB |
Output is correct |
9 |
Correct |
11 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23824 KB |
Output is correct |
11 |
Correct |
10 ms |
23896 KB |
Output is correct |
12 |
Correct |
10 ms |
23856 KB |
Output is correct |
13 |
Correct |
10 ms |
23844 KB |
Output is correct |
14 |
Correct |
11 ms |
23976 KB |
Output is correct |
15 |
Correct |
12 ms |
23896 KB |
Output is correct |
16 |
Correct |
12 ms |
23900 KB |
Output is correct |
17 |
Correct |
10 ms |
23896 KB |
Output is correct |
18 |
Correct |
11 ms |
23900 KB |
Output is correct |
19 |
Correct |
410 ms |
58528 KB |
Output is correct |
20 |
Correct |
357 ms |
49876 KB |
Output is correct |
21 |
Correct |
398 ms |
59796 KB |
Output is correct |
22 |
Correct |
11 ms |
23900 KB |
Output is correct |
23 |
Correct |
386 ms |
48308 KB |
Output is correct |
24 |
Correct |
387 ms |
50000 KB |
Output is correct |
25 |
Correct |
285 ms |
50892 KB |
Output is correct |
26 |
Correct |
11 ms |
23900 KB |
Output is correct |
27 |
Correct |
374 ms |
56104 KB |
Output is correct |
28 |
Correct |
356 ms |
47200 KB |
Output is correct |
29 |
Correct |
355 ms |
47624 KB |
Output is correct |
30 |
Correct |
315 ms |
48844 KB |
Output is correct |
31 |
Correct |
323 ms |
46876 KB |
Output is correct |
32 |
Correct |
221 ms |
46088 KB |
Output is correct |
33 |
Correct |
10 ms |
23896 KB |
Output is correct |
34 |
Correct |
10 ms |
23900 KB |
Output is correct |
35 |
Correct |
11 ms |
23900 KB |
Output is correct |
36 |
Correct |
10 ms |
23940 KB |
Output is correct |
37 |
Correct |
388 ms |
48448 KB |
Output is correct |
38 |
Incorrect |
386 ms |
49236 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |