#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);
map<str,int>mp;
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];
}
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
278 ms |
11412 KB |
Output is correct |
5 |
Correct |
292 ms |
11332 KB |
Output is correct |
6 |
Correct |
281 ms |
11328 KB |
Output is correct |
7 |
Correct |
314 ms |
11020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
274 ms |
11400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |