#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
const int maxn5 = 3e5 + 10;
int a[maxn5], req[maxn5];
int dp[2][maxn5]; // dp[0 -> khodesh nabashad/ 1 -> bashad][v]
int pd[maxn5], num[maxn5];
int sz[maxn5], parof[maxn5];
map <string, int> av;
bool mark[maxn5], incy[maxn5];
vector <int> adj[maxn5], ver, tmp;
int nn = 0, cmp[maxn5];
inline int dfs2(int v){
//cout << "hello " << v << endl;
mark[v] = true;
int cy = -1;
int u = parof[v];
cmp[v] = nn;
//cout << "ok " << v << ' ' << u << endl;
if(!mark[u]){
int z = dfs2(u);
//cout << "aha " << z << endl;
if(cy == -1)
cy = z;
}
else if(cmp[u] == cmp[v]){
cy = u;
}
//cout << ' ' << v << ' ' << cy << endl;
if(cy != -1)
incy[v] = true;
return (cy == v ? -1 : cy);
}
inline void dfs(int v){
//cout << "hello " << v << endl;
dp[0][v] = 0;
sz[v] = 1;
bool re = false;
for(auto u : adj[v]) if(!incy[u]){
dfs(u);
sz[v] += sz[u];
dp[0][v] += dp[1][u] + (sz[u] % 2);
re = true;
}
if(!re){
dp[1][v] = 0;
return;
}
dp[1][v] = maxn5;
for(auto u : adj[v]) if(!incy[u]){
int val = dp[0][v] - dp[1][u] - (sz[u] % 2);
val += dp[0][u] + (sz[u] % 2 == 0);
val++;
if(sz[v] % 2 == 1)
val--;
dp[1][v] = min(dp[1][v], val);
}
if(sz[v] % 2 == 0){
dp[0][v]--;
}
else{
dp[1][v] = min(dp[1][v], dp[0][v]);
}
//cout << v << ' ' << dp[0][v] << ' ' << dp[1][v] << endl;
return;
}
inline int find_next(int v){
return parof[v];
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
int ind = 0;
for(int i = 0; i < n; i++){
string s, t; cin >> s >> t;
if(av.find(s) == av.end())
av[s] = ind++;
if(av.find(t) == av.end())
av[t] = ind++;
adj[av[t]].pb(av[s]);
parof[av[s]] = av[t];
}
/*
for(auto it = av.begin(); it != av.end(); it++)
cout << "* " << (it -> fi) << ' ' << (it -> se) << endl;
//*/
if(n % 2 == 1)
return cout << -1 << endl, 0;
memset(mark, false, sizeof mark);
for(int i = 0; i < n; i++) if(!mark[i]){
dfs2(i);
nn++;
}
for(int i = 0; i < n; i++) if(incy[i]){
dfs(i);
}
//cout << "omg " << dp[1][3] << endl;
memset(mark, false, sizeof mark);
ll ans = 0;
for(int i = 0; i < n; i++) if(incy[i] && !mark[i]){
//cout << "oh a new day! " << i << endl;
ver.clear();
int v = find_next(i);
mark[i] = true;
ver.pb(i);
while(v != i){
mark[v] = true;
ver.pb(v);
//cout << v << ' ' << i << endl;
v = find_next(v);
}
if(ver.size() == 1){
ans += dp[1][i];
if(sz[i] % 2 == 1)
ans++;
continue;
}
pd[0] = dp[1][i];
num[0] = sz[i];
for(int j = 1; j < ver.size(); j++){
int u = ver[j];
num[j] = num[j - 1] + sz[u];
pd[j] = pd[j - 1] + dp[1][u] + 2 * (num[j - 1] % 2 == 1 && sz[u] % 2 == 1);
int val = (j == 1 ? 0 : pd[j - 2]) + dp[0][u] + dp[0][ver[j - 1]] + 1;
int f = (sz[u] % 2 == 0) + (sz[ver[j - 1]] % 2 == 0) + (j > 1 && num[j - 2] % 2 == 1);
if(f >= 2)
val += 2;
pd[j] = min(pd[j], val);
val = pd[j - 1] + dp[0][u] + 2 * (num[j - 1] % 2 == 1 && sz[u] % 2 == 0);
if(num[j] % 2 == 1)
pd[j] = min(pd[j], val);
}
int cur = pd[ver.size() - 1] + (num[ver.size() - 1] % 2);
//cout << "hmm " << cur << endl;
tmp.clear();
tmp.pb(ver.back());
for(auto u : ver)
tmp.pb(u);
tmp.pop_back();
ver.clear();
for(auto u : tmp)
ver.pb(u);
pd[0] = dp[1][ver[0]];
num[0] = sz[ver[0]];
//cout << "well " << pd[0] << endl;
for(int j = 1; j < ver.size(); j++){
int u = ver[j];
num[j] = num[j - 1] + sz[u];
pd[j] = pd[j - 1] + dp[1][u] + 2 * (num[j - 1] % 2 == 1 && sz[u] % 2 == 1);
//cout << "here tar " << pd[j] << ' ' << dp[1][u] << endl;
int val = (j == 1 ? 0 : pd[j - 2]) + dp[0][u] + dp[0][ver[j - 1]] + 1;
int f = (sz[u] % 2 == 0) + (sz[ver[j - 1]] % 2 == 0) + (j > 1 && num[j - 2] % 2 == 1);
if(f >= 2)
val += 2;
//cout << "here " << pd[j] << endl;
pd[j] = min(pd[j], val);
val = pd[j - 1] + dp[0][u] + 2 * (num[j - 1] % 2 == 1 && sz[u] % 2 == 0);
if(num[j] % 2 == 1)
pd[j] = min(pd[j], val);
//cout << j << ' ' << ver[j] << ' ' << pd[j] << ' ' << num[j] << ' ' << sz[u] << endl;
}
cur = min(cur, pd[ver.size() - 1] + (num[ver.size() - 1] % 2));
//cout << "ok " << cur << endl;
if(ver.size() == 2){
cur = min(cur, dp[0][ver[0]] + dp[0][ver[1]] + (sz[ver[0]] % 2 == 0) + (sz[ver[1]] % 2 == 0));
}
ans += cur;
}
cout << ans << endl;
}
/*
6
0 2
1 5
2 5
3 5
4 1
5 1
*/
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:135:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(int j = 1; j < ver.size(); j++){
| ~~^~~~~~~~~~~~
polygon.cpp:161:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
161 | for(int j = 1; j < ver.size(); j++){
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7628 KB |
Output is correct |
2 |
Correct |
4 ms |
7628 KB |
Output is correct |
3 |
Correct |
4 ms |
7628 KB |
Output is correct |
4 |
Correct |
4 ms |
7628 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7316 KB |
Output is correct |
7 |
Correct |
4 ms |
7628 KB |
Output is correct |
8 |
Correct |
6 ms |
7628 KB |
Output is correct |
9 |
Correct |
4 ms |
7628 KB |
Output is correct |
10 |
Correct |
5 ms |
7628 KB |
Output is correct |
11 |
Correct |
4 ms |
7628 KB |
Output is correct |
12 |
Correct |
4 ms |
7628 KB |
Output is correct |
13 |
Correct |
4 ms |
7628 KB |
Output is correct |
14 |
Correct |
4 ms |
7628 KB |
Output is correct |
15 |
Correct |
4 ms |
7628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7628 KB |
Output is correct |
2 |
Correct |
5 ms |
7628 KB |
Output is correct |
3 |
Correct |
4 ms |
7628 KB |
Output is correct |
4 |
Correct |
254 ms |
24192 KB |
Output is correct |
5 |
Correct |
302 ms |
20680 KB |
Output is correct |
6 |
Correct |
241 ms |
24636 KB |
Output is correct |
7 |
Correct |
280 ms |
18652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
19808 KB |
Output is correct |
2 |
Correct |
261 ms |
21832 KB |
Output is correct |
3 |
Correct |
211 ms |
20636 KB |
Output is correct |
4 |
Correct |
216 ms |
17096 KB |
Output is correct |
5 |
Correct |
253 ms |
27744 KB |
Output is correct |
6 |
Correct |
240 ms |
19140 KB |
Output is correct |
7 |
Correct |
233 ms |
19140 KB |
Output is correct |
8 |
Correct |
243 ms |
19396 KB |
Output is correct |
9 |
Correct |
220 ms |
18592 KB |
Output is correct |
10 |
Correct |
159 ms |
17848 KB |
Output is correct |
11 |
Correct |
5 ms |
7664 KB |
Output is correct |
12 |
Correct |
5 ms |
7628 KB |
Output is correct |
13 |
Correct |
5 ms |
7640 KB |
Output is correct |
14 |
Correct |
5 ms |
7628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7628 KB |
Output is correct |
2 |
Correct |
4 ms |
7628 KB |
Output is correct |
3 |
Correct |
4 ms |
7628 KB |
Output is correct |
4 |
Correct |
4 ms |
7628 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7316 KB |
Output is correct |
7 |
Correct |
4 ms |
7628 KB |
Output is correct |
8 |
Correct |
6 ms |
7628 KB |
Output is correct |
9 |
Correct |
4 ms |
7628 KB |
Output is correct |
10 |
Correct |
5 ms |
7628 KB |
Output is correct |
11 |
Correct |
4 ms |
7628 KB |
Output is correct |
12 |
Correct |
4 ms |
7628 KB |
Output is correct |
13 |
Correct |
4 ms |
7628 KB |
Output is correct |
14 |
Correct |
4 ms |
7628 KB |
Output is correct |
15 |
Correct |
4 ms |
7628 KB |
Output is correct |
16 |
Correct |
4 ms |
7628 KB |
Output is correct |
17 |
Correct |
5 ms |
7628 KB |
Output is correct |
18 |
Correct |
4 ms |
7628 KB |
Output is correct |
19 |
Correct |
254 ms |
24192 KB |
Output is correct |
20 |
Correct |
302 ms |
20680 KB |
Output is correct |
21 |
Correct |
241 ms |
24636 KB |
Output is correct |
22 |
Correct |
280 ms |
18652 KB |
Output is correct |
23 |
Correct |
242 ms |
19808 KB |
Output is correct |
24 |
Correct |
261 ms |
21832 KB |
Output is correct |
25 |
Correct |
211 ms |
20636 KB |
Output is correct |
26 |
Correct |
216 ms |
17096 KB |
Output is correct |
27 |
Correct |
253 ms |
27744 KB |
Output is correct |
28 |
Correct |
240 ms |
19140 KB |
Output is correct |
29 |
Correct |
233 ms |
19140 KB |
Output is correct |
30 |
Correct |
243 ms |
19396 KB |
Output is correct |
31 |
Correct |
220 ms |
18592 KB |
Output is correct |
32 |
Correct |
159 ms |
17848 KB |
Output is correct |
33 |
Correct |
5 ms |
7664 KB |
Output is correct |
34 |
Correct |
5 ms |
7628 KB |
Output is correct |
35 |
Correct |
5 ms |
7640 KB |
Output is correct |
36 |
Correct |
5 ms |
7628 KB |
Output is correct |
37 |
Correct |
260 ms |
22604 KB |
Output is correct |
38 |
Correct |
350 ms |
22112 KB |
Output is correct |
39 |
Correct |
253 ms |
21340 KB |
Output is correct |
40 |
Correct |
238 ms |
21316 KB |
Output is correct |
41 |
Correct |
236 ms |
21188 KB |
Output is correct |
42 |
Correct |
270 ms |
21864 KB |
Output is correct |
43 |
Correct |
237 ms |
21704 KB |
Output is correct |
44 |
Correct |
243 ms |
21748 KB |
Output is correct |
45 |
Correct |
264 ms |
21676 KB |
Output is correct |
46 |
Correct |
241 ms |
21652 KB |
Output is correct |
47 |
Correct |
237 ms |
20856 KB |
Output is correct |
48 |
Correct |
240 ms |
21948 KB |
Output is correct |
49 |
Correct |
268 ms |
23988 KB |
Output is correct |
50 |
Correct |
208 ms |
22724 KB |
Output is correct |
51 |
Correct |
227 ms |
19268 KB |
Output is correct |
52 |
Correct |
280 ms |
30020 KB |
Output is correct |
53 |
Correct |
234 ms |
21184 KB |
Output is correct |
54 |
Correct |
245 ms |
21232 KB |
Output is correct |
55 |
Correct |
245 ms |
21772 KB |
Output is correct |
56 |
Correct |
223 ms |
20644 KB |
Output is correct |
57 |
Correct |
159 ms |
19928 KB |
Output is correct |
58 |
Correct |
4 ms |
7628 KB |
Output is correct |
59 |
Correct |
4 ms |
7628 KB |
Output is correct |
60 |
Correct |
5 ms |
7628 KB |
Output is correct |
61 |
Correct |
4 ms |
7628 KB |
Output is correct |
62 |
Correct |
4 ms |
7628 KB |
Output is correct |
63 |
Correct |
4 ms |
7628 KB |
Output is correct |
64 |
Correct |
4 ms |
7604 KB |
Output is correct |
65 |
Correct |
262 ms |
26440 KB |
Output is correct |
66 |
Correct |
254 ms |
22832 KB |
Output is correct |
67 |
Correct |
258 ms |
26816 KB |
Output is correct |
68 |
Correct |
253 ms |
20932 KB |
Output is correct |
69 |
Correct |
6 ms |
7628 KB |
Output is correct |
70 |
Correct |
4 ms |
7628 KB |
Output is correct |
71 |
Correct |
4 ms |
7636 KB |
Output is correct |
72 |
Correct |
4 ms |
7628 KB |
Output is correct |
73 |
Correct |
3 ms |
7372 KB |
Output is correct |
74 |
Correct |
4 ms |
7384 KB |
Output is correct |
75 |
Correct |
4 ms |
7628 KB |
Output is correct |
76 |
Correct |
4 ms |
7636 KB |
Output is correct |
77 |
Correct |
4 ms |
7628 KB |
Output is correct |
78 |
Correct |
4 ms |
7628 KB |
Output is correct |
79 |
Correct |
4 ms |
7628 KB |
Output is correct |
80 |
Correct |
4 ms |
7628 KB |
Output is correct |
81 |
Correct |
5 ms |
7636 KB |
Output is correct |
82 |
Correct |
4 ms |
7628 KB |
Output is correct |
83 |
Correct |
4 ms |
7628 KB |
Output is correct |