#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], ans;
vector<int>g[maxn];
int vis[maxn], mark[maxn], mark2[maxn][2], dp[maxn][2];
int dfs(int u, bool mode){
if(mark2[u][mode])return dp[u][mode];
ckmax(vis[u],1);
mark2[u][mode] = 1;
ckmax(vis[u],1);
for(auto v:g[u]){
if(vis[v]!=2){
if(mode)dp[u][1] += dfs(v,0);
else dp[u][0] += max(dfs(v,0),dfs(v,1));
}
}
return dp[u][mode] = dp[u][mode] + mode;
}
int main(){_
int n;cin>>n;
if(n&1){
cout<<-1<<endl;
return 0;
}
int q = 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;
cur = par[cur];
while(cur!=start){
vis[cur] = 2, cur = par[cur], ++cnt;
}
vis[start] = 2;
if(cnt==2){
mark[cur] = mark[par[cur]] = 1;
for(auto v:g[cur])if(vis[v]!=2)ans += dfs(v,0);
for(auto v:g[par[cur]])if(vis[v]!=2)ans += dfs(v,0);
}else{
cur = par[start];
if(dfs(start,1)>dfs(start,0))mark[start] = 1;
ans += max(dfs(start,1),dfs(start,0));
while(cur!=start){
if(sz(g[cur])!=1){
if(dfs(cur,1)>dfs(cur,0))mark[cur] = 1;
ans += max(dfs(cur,1),dfs(cur,0));
}
cur = par[cur];
}
cur = par[start];
while(cur!=start&&!mark[cur])cur = par[cur];
start = cur, cur = par[cur], cnt = 0;
while(cur!=start){
if(sz(g[cur])!=1)ans += (cnt+1)/2, cnt = 0;
else cnt++;
cur = par[cur];
}
ans += (cnt+1)/2;
}
}
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
255 ms |
15512 KB |
Output is correct |
2 |
Correct |
261 ms |
17580 KB |
Output is correct |
3 |
Correct |
164 ms |
16240 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
236 ms |
24608 KB |
Output is correct |
6 |
Correct |
220 ms |
14396 KB |
Output is correct |
7 |
Correct |
222 ms |
14716 KB |
Output is correct |
8 |
Correct |
197 ms |
15300 KB |
Output is correct |
9 |
Correct |
199 ms |
14016 KB |
Output is correct |
10 |
Correct |
144 ms |
13152 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
1 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |