#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];
void dfs(int u, int p){
ckmax(vis[u],1);
for(auto v:g[u]){
if(vis[v]!=2)dfs(v,u);
}
if(!mark[u]&&u!=p)ans++, mark[p] = 1;
}
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,cur = par[cur];
if(cnt==2)mark[cur] = mark[par[cur]] = 1;
dfs(start,start);
while(cur!=start){
dfs(cur,cur);
cur = par[cur];
}
if(!mark[start])ans++, mark[par[start]] = 1;
cur = par[start];
while(cur!=start){
if(!mark[cur])ans++, mark[par[cur]] = 1;
cur = par[cur];
}
}
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2696 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 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 |
2 ms |
2692 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2692 KB |
Output is correct |
3 |
Correct |
2 ms |
2664 KB |
Output is correct |
4 |
Correct |
290 ms |
16932 KB |
Output is correct |
5 |
Correct |
297 ms |
16864 KB |
Output is correct |
6 |
Correct |
296 ms |
16856 KB |
Output is correct |
7 |
Correct |
3 ms |
2696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
16164 KB |
Output is correct |
2 |
Correct |
277 ms |
17844 KB |
Output is correct |
3 |
Correct |
222 ms |
16832 KB |
Output is correct |
4 |
Correct |
2 ms |
2704 KB |
Output is correct |
5 |
Correct |
286 ms |
22604 KB |
Output is correct |
6 |
Correct |
280 ms |
15316 KB |
Output is correct |
7 |
Correct |
288 ms |
15312 KB |
Output is correct |
8 |
Correct |
227 ms |
15772 KB |
Output is correct |
9 |
Correct |
307 ms |
14868 KB |
Output is correct |
10 |
Correct |
157 ms |
13748 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2648 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2696 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 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 |
2 ms |
2692 KB |
Output is correct |
15 |
Correct |
2 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
3 ms |
2692 KB |
Output is correct |
18 |
Correct |
2 ms |
2664 KB |
Output is correct |
19 |
Correct |
290 ms |
16932 KB |
Output is correct |
20 |
Correct |
297 ms |
16864 KB |
Output is correct |
21 |
Correct |
296 ms |
16856 KB |
Output is correct |
22 |
Correct |
3 ms |
2696 KB |
Output is correct |
23 |
Correct |
287 ms |
16164 KB |
Output is correct |
24 |
Correct |
277 ms |
17844 KB |
Output is correct |
25 |
Correct |
222 ms |
16832 KB |
Output is correct |
26 |
Correct |
2 ms |
2704 KB |
Output is correct |
27 |
Correct |
286 ms |
22604 KB |
Output is correct |
28 |
Correct |
280 ms |
15316 KB |
Output is correct |
29 |
Correct |
288 ms |
15312 KB |
Output is correct |
30 |
Correct |
227 ms |
15772 KB |
Output is correct |
31 |
Correct |
307 ms |
14868 KB |
Output is correct |
32 |
Correct |
157 ms |
13748 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2648 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
289 ms |
16640 KB |
Output is correct |
38 |
Incorrect |
257 ms |
15540 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |