#include <bits/stdc++.h>
#define maxn (int)(1e5+51)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define inf 2000000001
#define mod 1000000007 //998244353
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
map<string,int>mp;
int par[maxn];
vector<int>g[maxn];
int vis[maxn], mark[maxn][2][2], dp[maxn][2][2];
int countnodes(int u){
vis[u] = 1;
int resp = 1;
for(auto v:g[u]){
resp += countnodes(v);
}
return resp;
}
int dfs(int u,bool mode,bool type){
if(mark[u][mode][type])return dp[u][mode][type];
mark[u][mode][type] = 1;
if(mode)dp[u][mode][type] = 1;
else{
dp[u][mode][type] = dfs(u,1,type) - 1;
}
int best = 0;
for(auto v:g[u]){
if(mode){
dp[u][mode][type] += dfs(v,0,type);
}else{
ckmax(best,dfs(v,1,type) - dfs(v,0,type));
}
}
dp[u][mode][type] += best;
return dp[u][mode][type];
}
void removeAdj(int x){
for(int i=0;i<sz(g[par[x]]);++i){
if(g[par[x]][i]==x){
g[par[x]].erase(g[par[x]].begin()+i);
break;
}
}
}
int main(){_
int n;cin>>n;
if(n&1){
cout<<-1<<endl;
return 0;
}
int q = 0, resp = 0;
for(int i=1;i<=n;++i){
string a,b;cin>>a>>b;
if(mp.find(a)==mp.end())mp[a]=++q;
if(mp.find(b)==mp.end())mp[b]=++q;
int u = mp[a], v = mp[b];
par[u] = v;
g[v].pb(u);
}
for(int i=1;i<=n;++i){
if(!vis[i]){
int cur = i;
while(!vis[cur])vis[cur] = 1, cur = par[cur];
int start = cur, cnt = 1, ans = 0, nodes = 0, last = cur;
cur = par[cur];
while(cur!=start)last = cur,cur = par[cur], ++cnt;
removeAdj(start);
nodes = countnodes(start);
if(cnt==1){
ans = dfs(start,0,0);
}else if(cnt==2){
removeAdj(par[start]);
for(auto v:g[start])ans += dfs(v,0,0);
for(auto v:g[par[start]])ans += dfs(v,0,0);
int ans2 = dfs(start,0,1) + dfs(par[start],0,1);
ans += 2;
ckmax(ans,ans2);
}else{
ans = dfs(start,0,0);//start is shot
int ans2 = 1;
removeAdj(par[start]);
removeAdj(last);
for(auto v:g[start])ans2 += dfs(v,0,1);
for(auto v:g[par[start]])ans2 += dfs(v,0,1);
ans2 += dfs(last,0,1);
ckmax(ans,ans2);
}
resp += nodes - ans;
}
}
cout<<resp<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2684 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
323 ms |
36472 KB |
Output is correct |
5 |
Correct |
261 ms |
19288 KB |
Output is correct |
6 |
Correct |
310 ms |
41208 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
16668 KB |
Output is correct |
2 |
Correct |
239 ms |
21784 KB |
Output is correct |
3 |
Correct |
176 ms |
17488 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
286 ms |
37692 KB |
Output is correct |
6 |
Correct |
240 ms |
16020 KB |
Output is correct |
7 |
Correct |
262 ms |
15952 KB |
Output is correct |
8 |
Correct |
201 ms |
16332 KB |
Output is correct |
9 |
Correct |
255 ms |
15500 KB |
Output is correct |
10 |
Correct |
139 ms |
14784 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
1 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
3 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2684 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
323 ms |
36472 KB |
Output is correct |
20 |
Correct |
261 ms |
19288 KB |
Output is correct |
21 |
Correct |
310 ms |
41208 KB |
Output is correct |
22 |
Correct |
3 ms |
2644 KB |
Output is correct |
23 |
Correct |
272 ms |
16668 KB |
Output is correct |
24 |
Correct |
239 ms |
21784 KB |
Output is correct |
25 |
Correct |
176 ms |
17488 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
286 ms |
37692 KB |
Output is correct |
28 |
Correct |
240 ms |
16020 KB |
Output is correct |
29 |
Correct |
262 ms |
15952 KB |
Output is correct |
30 |
Correct |
201 ms |
16332 KB |
Output is correct |
31 |
Correct |
255 ms |
15500 KB |
Output is correct |
32 |
Correct |
139 ms |
14784 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
1 ms |
2644 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
303 ms |
19032 KB |
Output is correct |
38 |
Correct |
242 ms |
21424 KB |
Output is correct |
39 |
Correct |
290 ms |
18272 KB |
Output is correct |
40 |
Correct |
272 ms |
18100 KB |
Output is correct |
41 |
Correct |
237 ms |
18140 KB |
Output is correct |
42 |
Correct |
324 ms |
18616 KB |
Output is correct |
43 |
Correct |
234 ms |
18740 KB |
Output is correct |
44 |
Correct |
309 ms |
18700 KB |
Output is correct |
45 |
Correct |
231 ms |
18688 KB |
Output is correct |
46 |
Correct |
276 ms |
18632 KB |
Output is correct |
47 |
Correct |
289 ms |
17772 KB |
Output is correct |
48 |
Correct |
244 ms |
18928 KB |
Output is correct |
49 |
Correct |
276 ms |
24004 KB |
Output is correct |
50 |
Correct |
173 ms |
19684 KB |
Output is correct |
51 |
Correct |
2 ms |
2700 KB |
Output is correct |
52 |
Correct |
268 ms |
40016 KB |
Output is correct |
53 |
Correct |
287 ms |
18252 KB |
Output is correct |
54 |
Correct |
210 ms |
18140 KB |
Output is correct |
55 |
Correct |
200 ms |
18412 KB |
Output is correct |
56 |
Correct |
236 ms |
17692 KB |
Output is correct |
57 |
Correct |
132 ms |
16832 KB |
Output is correct |
58 |
Correct |
2 ms |
2644 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2696 KB |
Output is correct |
61 |
Correct |
2 ms |
2644 KB |
Output is correct |
62 |
Correct |
2 ms |
2644 KB |
Output is correct |
63 |
Correct |
2 ms |
2692 KB |
Output is correct |
64 |
Correct |
2 ms |
2684 KB |
Output is correct |
65 |
Correct |
354 ms |
38600 KB |
Output is correct |
66 |
Correct |
243 ms |
19616 KB |
Output is correct |
67 |
Correct |
316 ms |
41500 KB |
Output is correct |
68 |
Correct |
2 ms |
2644 KB |
Output is correct |
69 |
Correct |
2 ms |
2644 KB |
Output is correct |
70 |
Correct |
2 ms |
2644 KB |
Output is correct |
71 |
Correct |
2 ms |
2644 KB |
Output is correct |
72 |
Correct |
2 ms |
2644 KB |
Output is correct |
73 |
Correct |
2 ms |
2688 KB |
Output is correct |
74 |
Correct |
2 ms |
2644 KB |
Output is correct |
75 |
Correct |
2 ms |
2644 KB |
Output is correct |
76 |
Correct |
2 ms |
2644 KB |
Output is correct |
77 |
Correct |
2 ms |
2644 KB |
Output is correct |
78 |
Correct |
2 ms |
2644 KB |
Output is correct |
79 |
Correct |
2 ms |
2688 KB |
Output is correct |
80 |
Correct |
2 ms |
2644 KB |
Output is correct |
81 |
Correct |
2 ms |
2644 KB |
Output is correct |
82 |
Correct |
2 ms |
2644 KB |
Output is correct |
83 |
Correct |
2 ms |
2612 KB |
Output is correct |