#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
vector<int>adj[100005];
map<string,int>mp;
int ptr=0;
int sz=0;
bool visited[100005];
void dfs(int index){
sz++;
visited[index]=true;
for(auto it:adj[index]){
if(visited[it]) continue;
dfs(it);
}
}
void solve(){
int n;
cin >> n;
string s,s2;
for(int x=0;x<n;x++){
cin >> s >> s2;
if(mp.find(s)==mp.end()){
mp[s]=ptr++;
}
if(mp.find(s2)==mp.end()){
mp[s2]=ptr++;
}
adj[mp[s]].push_back(mp[s2]);
adj[mp[s2]].push_back(mp[s]);
}
int counter=0;
for(int x=0;x<ptr;x++){
if(visited[x]) continue;
dfs(x);
counter+=(sz+1)/2;
}
if(n%2) cout << -1;
else cout << counter;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2812 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
16404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2812 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |