#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);
map<str,int>mp;
vi vis(MX,0);
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]]++;
}
if(N&1){
cout << -1 << endl;
return 0;
}
set<int>s;
FOR(i,0,N) if(in[i]==0) s.insert(i);
int ans=0;
while(sz(s)){
auto it=s.begin();
int u=*it;
s.erase(it);
if(out[u]==u) continue;
if(!vis[out[u]]){
if(out[out[u]]!=out[u]){
in[out[out[u]]]--;
if(in[out[out[u]]]==0) s.insert(out[out[u]]);
}
vis[u]=1; vis[out[u]]=1;
out[out[u]]=u;
ans++;
}
}
FOR(i,0,N) if(!vis[i] && vis[out[i]]) out[i]=i;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
314 ms |
10380 KB |
Output is correct |
5 |
Correct |
318 ms |
10416 KB |
Output is correct |
6 |
Correct |
299 ms |
10368 KB |
Output is correct |
7 |
Correct |
317 ms |
10388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
328 ms |
11608 KB |
Output is correct |
2 |
Correct |
340 ms |
11180 KB |
Output is correct |
3 |
Correct |
274 ms |
10416 KB |
Output is correct |
4 |
Correct |
289 ms |
10416 KB |
Output is correct |
5 |
Correct |
307 ms |
10588 KB |
Output is correct |
6 |
Correct |
307 ms |
12744 KB |
Output is correct |
7 |
Correct |
318 ms |
12896 KB |
Output is correct |
8 |
Correct |
292 ms |
12228 KB |
Output is correct |
9 |
Correct |
292 ms |
13808 KB |
Output is correct |
10 |
Correct |
264 ms |
15100 KB |
Output is correct |
11 |
Correct |
2 ms |
2640 KB |
Output is correct |
12 |
Correct |
2 ms |
2636 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |