This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
map<string,int> m;
vector<int> vis(N);
vector<int> v(N),in(N);
int cnt=0;
int id(string s){
if(m.count(s)){
return m[s];
}
int p=m.size();
m[s]=p;
return p;
}
void dfs(int k){
vis[k]=1;
cnt++;
if(!vis[v[k]]){
dfs(v[k]);
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
if(n&1){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<n;i++){
string a,b;cin>>a>>b;
v[id(a)]=id(b);
in[id(b)]++;
}
int ans=0;
for(int i=0;i<n;i++){
if(!vis[i]&&v[v[i]]==i&&v[i]!=i){
vis[i]=1;
vis[v[i]]=1;
}
}
for(int i=0;i<n;i++){
if(in[i]==0&&vis[i]==0){
cnt=0;
dfs(i);
ans+=(cnt+1)/2;
}
}
for(int i=0;i<n;i++){
if(!vis[i]&&v[i]==i){
ans++;
}
}
cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |