#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];
if(s!=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
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) ans+=(vis[i]==0);
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Incorrect |
5 ms |
7244 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Incorrect |
5 ms |
7244 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
494 ms |
19396 KB |
Output is correct |
2 |
Correct |
516 ms |
21188 KB |
Output is correct |
3 |
Correct |
398 ms |
25204 KB |
Output is correct |
4 |
Correct |
474 ms |
20648 KB |
Output is correct |
5 |
Correct |
490 ms |
20420 KB |
Output is correct |
6 |
Correct |
497 ms |
22792 KB |
Output is correct |
7 |
Correct |
496 ms |
22980 KB |
Output is correct |
8 |
Correct |
442 ms |
23312 KB |
Output is correct |
9 |
Correct |
456 ms |
24104 KB |
Output is correct |
10 |
Correct |
368 ms |
25492 KB |
Output is correct |
11 |
Correct |
5 ms |
7308 KB |
Output is correct |
12 |
Correct |
5 ms |
7372 KB |
Output is correct |
13 |
Correct |
5 ms |
7248 KB |
Output is correct |
14 |
Correct |
5 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7244 KB |
Output is correct |
6 |
Correct |
5 ms |
7372 KB |
Output is correct |
7 |
Incorrect |
5 ms |
7244 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |