#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];
if(s!=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){
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 |
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 |
2640 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 |
- |
# |
Verdict |
Execution time |
Memory |
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 |
383 ms |
10488 KB |
Output is correct |
5 |
Correct |
331 ms |
11000 KB |
Output is correct |
6 |
Correct |
314 ms |
10836 KB |
Output is correct |
7 |
Correct |
323 ms |
10868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
334 ms |
11756 KB |
Output is correct |
2 |
Correct |
332 ms |
11172 KB |
Output is correct |
3 |
Correct |
264 ms |
15236 KB |
Output is correct |
4 |
Correct |
339 ms |
10524 KB |
Output is correct |
5 |
Correct |
322 ms |
10536 KB |
Output is correct |
6 |
Correct |
334 ms |
12868 KB |
Output is correct |
7 |
Correct |
338 ms |
12804 KB |
Output is correct |
8 |
Correct |
313 ms |
13460 KB |
Output is correct |
9 |
Correct |
306 ms |
13836 KB |
Output is correct |
10 |
Correct |
273 ms |
15212 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 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 |
2664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2640 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 |
- |