#include<bits/stdc++.h>
using namespace std;
map<string,int>mp;
vector<int>v[100005];
vector<int>rt;
int dp[100005][2];
int dp2[100005][2];
int sz[100005];
int vis[100005];
int lp=0;
int p[100005];
int com[100005];
int fp(int a){return p[a]==a?a:p[a]=fp(p[a]);}
void un(int a,int b){p[fp(b)]=fp(a);}
void dfs(int u,int rt){
//cerr<<"node"<<u<<"\n";
if(vis[u])return;
vis[u]=1;
int sum=0;
sz[u]=1;
for(auto x:v[u]){
if(vis[x])continue;
dfs(x,rt);
sz[u]+=sz[x];
sum+=dp[x][0];
}
dp[u][1]=sum;
for(auto x:v[u])if(x!=rt)dp[u][0]=max(sum-dp[x][0]+dp[x][1]+1,dp[u][0]);
}
void dfs2(int u){
if(u==lp)return;
int sum=0;
for(auto x:v[u]){
dfs2(x);
sum+=dp2[x][0];
}
dp2[u][1]=sum;
for(auto x:v[u])dp2[u][0]=max(sum-dp2[x][0]+dp2[x][1]+1,dp2[u][0]);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
int cur=0;
for(int i=1;i<=n;i++)p[i]=i;
for(int i=0;i<n;i++){
string a,b;cin>>a>>b;
if(mp[a]==0)cur++,mp[a]=cur;
if(mp[b]==0)cur++,mp[b]=cur;
//cerr<<a<<" "<<mp[a]<<","<<b<<" "<<mp[b]<<"\n";
if(mp[a]==mp[b])rt.push_back(mp[a])/*,cerr<<"root\n"*/;
else if(fp(mp[a])==fp(mp[b]))rt.push_back(mp[a]),com[mp[a]]=mp[b]/*,cerr<<"one loop\n"*/;
else v[mp[b]].push_back(mp[a])/*,cerr<<"nothing\n"*/;
un(mp[a],mp[b]);
}
if(n%2==1){
cout<<"-1";
return 0;
}
/*for(int i=1;i<=n;i++){
cerr<<i<<": ";
for(auto x:v[i])cerr<<x<<" ";
cerr<<"\n";
}*/
int ans=0;
//for(auto x:rt)cerr<<x<<" ";
//cerr<<"\n";
for(auto x:rt){
//cerr<<x<<":\n";
dfs(x,x);
//cerr<<dp[x][0]<<" "<<dp[x][1]<<"\n";
int temp=(max(dp[x][0],dp[x][1])+sz[x]-2*max(dp[x][0],dp[x][1]));
//cerr<<"temp:"<<temp<<"\n";
if(com[x]!=0){
//cerr<<"loop\n";
lp=com[x];
//cerr<<lp<<"\n";
dfs2(x);
int pp=dp2[x][1]+dp[lp][1]+1;
if(sz[x]!=2)ans+=min(temp,pp+(sz[x]-pp*2));
}else ans+=(max(dp[x][0],dp[x][1])+sz[x]-2*max(dp[x][0],dp[x][1]));
//cerr<<max(dp[x][0],dp[x][1])<<"\n";
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5720 KB |
Output is correct |
3 |
Correct |
1 ms |
5724 KB |
Output is correct |
4 |
Correct |
1 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
1 ms |
5724 KB |
Output is correct |
7 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
1 ms |
5724 KB |
Output is correct |
4 |
Incorrect |
285 ms |
23360 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
299 ms |
16020 KB |
Output is correct |
2 |
Correct |
263 ms |
17748 KB |
Output is correct |
3 |
Correct |
173 ms |
13804 KB |
Output is correct |
4 |
Correct |
276 ms |
15476 KB |
Output is correct |
5 |
Correct |
280 ms |
23832 KB |
Output is correct |
6 |
Correct |
276 ms |
15184 KB |
Output is correct |
7 |
Correct |
256 ms |
15192 KB |
Output is correct |
8 |
Correct |
214 ms |
14796 KB |
Output is correct |
9 |
Correct |
259 ms |
14640 KB |
Output is correct |
10 |
Correct |
186 ms |
13880 KB |
Output is correct |
11 |
Correct |
2 ms |
5724 KB |
Output is correct |
12 |
Correct |
2 ms |
5724 KB |
Output is correct |
13 |
Correct |
1 ms |
5724 KB |
Output is correct |
14 |
Correct |
2 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5720 KB |
Output is correct |
2 |
Correct |
1 ms |
5720 KB |
Output is correct |
3 |
Correct |
1 ms |
5724 KB |
Output is correct |
4 |
Correct |
1 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
1 ms |
5724 KB |
Output is correct |
7 |
Incorrect |
1 ms |
5724 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |