#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; --i)
#define trav(a, x) for (auto& a : x)
#define sz(a) a.size()
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
unordered_map<string, string> people;
unordered_set<string> vis;
int si;
void dfs(string person){
if(vis.count(person))return;
vis.insert(person);
++si;
dfs(people[person]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
if (n & 1) {
cout << -1 << endl;
return 0;
}
int ans=0;
string n1, n2;
rep(i, 0, n) {
cin >> n1 >> n2;
people.emplace(n1, n2);
if (n1!=n2&&people.count(n2) && people[n2] == n1) {
people.erase(n1);
people.erase(n2);
}
}
trav(person,people){
si=0;
dfs(person.first);
ans+=(si+1)>>1;
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
194 ms |
30868 KB |
Output is correct |
5 |
Correct |
158 ms |
19304 KB |
Output is correct |
6 |
Correct |
188 ms |
32716 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
175 ms |
20056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |