#include "bits/stdc++.h"
using namespace std;
// Hi
const int Mx = 1e5+5;
struct DSU{
int n;
vector<int> parent,sz;
DSU(int _n){
n = _n;
for(int i=0;i<=n;i++){
parent.push_back(i);
sz.push_back(1);
}
}
int find(int x){
return parent[x] = (parent[x] == x ? x : find(parent[x]));
}
void unite(int a,int b){
a = find(a);
b = find(b);
if(a != b){
if(sz[a] >= sz[b]){
parent[b] = a;
sz[a] += sz[b];
}else{
parent[a] = b;
sz[b] += sz[a];
}
}
}
};
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int n;
cin>>n;
DSU st(n);
vector<int> go(n);
map<string,int> mp;
int nxt = 0;
for(int i=0;i<n;i++){
string s,t;
cin>>s>>t;
int first = 0,second = 0;
if(mp.find(s) != mp.end()){
first = mp[s];
}else{
first = nxt;
mp[s] = nxt++;
}
if(mp.find(t) != mp.end()){
second = mp[t];
}else{
second = nxt;
mp[t] = nxt++;
}
go[first] = second;
st.unite(first,second);
}
if(n&1){
cout << -1 << '\n';
return 0;
}
int ans = 0;
vector<int> si;
for(int i=0;i<n;i++){
if(st.find(i) == i){
if(st.sz[i] & 1)ans++;
ans += st.sz[i]/2;
}
}
int cnt = 0;
for(int i=0;i<n;i++){
if(go[go[i]] == i && go[i] != i)cnt++;
if(go[i] == i)ans++;
}
ans -= cnt/2;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
202 ms |
11468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |