#include<bits/stdc++.h>
using namespace std;
const int N=100005;
map<string,int> m;
vector<int> vis(N);
vector<int> v(N,-1),in(N);
inline int id(string s){
if(m.count(s)){
return m[s];
}
int p=m.size();
m[s]=p;
return p;
}
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)]++;
if(v[id(b)]==id(a)&&a!=b){
vis[id(a)]=vis[id(b)]=1;
}
}
int ans=0;
queue<int> q;
for(int i=0;i<n;i++){
if(in[i]==0){
q.push(i);
}
}
while(q.size()){
int p=q.front();
q.pop();
//cout<<p<<endl;
in[v[p]]--;
if(in[v[p]]==0){
q.push(v[p]);
}
if(vis[p]||vis[v[p]]){
continue;
}
ans++;
vis[p]=vis[v[p]]=1;
}
for(int i=0;i<n;i++){
if(vis[i])continue;
int sz=0,f=i;
while(vis[f]==0){
sz++;
vis[f]=1;
f=v[f];
}
//assert(sz==1);
ans+=(sz+1)/2;
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1640 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1628 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
214 ms |
11720 KB |
Output is correct |
5 |
Correct |
250 ms |
11560 KB |
Output is correct |
6 |
Correct |
208 ms |
11400 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
9556 KB |
Output is correct |
2 |
Correct |
205 ms |
9296 KB |
Output is correct |
3 |
Correct |
185 ms |
9280 KB |
Output is correct |
4 |
Correct |
1 ms |
1828 KB |
Output is correct |
5 |
Correct |
219 ms |
9260 KB |
Output is correct |
6 |
Correct |
210 ms |
9476 KB |
Output is correct |
7 |
Correct |
220 ms |
9796 KB |
Output is correct |
8 |
Correct |
193 ms |
9568 KB |
Output is correct |
9 |
Correct |
195 ms |
9552 KB |
Output is correct |
10 |
Correct |
159 ms |
9916 KB |
Output is correct |
11 |
Correct |
1 ms |
1640 KB |
Output is correct |
12 |
Correct |
1 ms |
1628 KB |
Output is correct |
13 |
Correct |
1 ms |
1628 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
1628 KB |
Output is correct |
3 |
Correct |
1 ms |
1628 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
1628 KB |
Output is correct |
6 |
Correct |
1 ms |
1640 KB |
Output is correct |
7 |
Correct |
1 ms |
1628 KB |
Output is correct |
8 |
Incorrect |
1 ms |
1628 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |