#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<int>vi;
#define pb push_back
#define sz(v) (int)v.size()
typedef string str;
#define FOR(i,a,b) for(int i=a; i<b; i++)
void IO(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
//------------------------------------------------------//
void ckmin(int &x, int y){x=min(x,y);}
const int MX=2e5;
int N;
vi out(MX),in(MX,0),adj[MX];
map<str,int>mp;
vi vis(MX,0);
int n=0;
void dfs(int u){
vis[u]=1;
n++;
for(int v: adj[u]) if(!vis[v]) dfs(v);
}
int main(){
IO();
cin>>N;
int cnt=0;
FOR(i,0,N){
str s,ss; cin>>s>>ss;
if(!mp.count(s)) mp[s]=cnt++;
if(!mp.count(ss)) mp[ss]=cnt++;
out[mp[s]]=mp[ss];
in[mp[ss]]++;
adj[mp[s]].pb(mp[ss]);
adj[mp[ss]].pb(mp[s]);
}
if(N&1){
cout << -1 << endl;
return 0;
}
//Sub2
/*vi vis(N,0);
int ans=0;
FOR(i,0,N){
int n=0,cur=i;
while(!vis[cur]){
vis[cur]=1; n++;
cur=out[cur];
}
if(n!=2) ans+=(n+1)/2;
}
cout << ans << endl;*/
//Sub3
int ans=0;
FOR(i,0,N)if(!vis[i]){
n=0;
dfs(i);
ans+=(n+1)/2;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7372 KB |
Output is correct |
2 |
Incorrect |
5 ms |
7268 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
479 ms |
18296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7372 KB |
Output is correct |
2 |
Incorrect |
5 ms |
7268 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |