#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> g[N];
map<string , int> mp;
int ans , n , num;
bool deg[N] , vis[N] , ch;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 0 ; i < n ; i++){
string a , b;
cin >> a >> b;
if(mp.find(a) == mp.end()) mp[a] = num++;
if(mp.find(b) == mp.end()) mp[b] = num++;
g[mp[a]].push_back(mp[b]);
deg[mp[b]] = 1;
}
if(n & 1){
cout << -1 << '\n';
return 0;
}
for(int i = 1 ; i <= n ; i++)
ch &= deg[i];
if(ch){
vector<int> vec;
for(int i = 1 ; i <= n ; i++){
int len = 0 , ver = i;
while(!vis[ver]){
len++;
vis[ver] = 1;
ver = g[ver][0];
}
if(len == 2) continue;
vec.push_back(len);
}
int cnt = 0;
for(int i : vec){
ans += i / 2;
if(i & 1) cnt++;
}
ans += cnt - 1;
cout << ans << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
223 ms |
13712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |