#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9;
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, z = 0;
cin >> n;
if (n % 2) {
cout << -1;
return 0;
}
vector<pair<string, string>> adj(n);
for (auto &it: adj) cin >> it.first >> it.second;
map<string, int> mp;
for (auto &it: adj) mp[it.first];
for (auto &it: adj) mp[it.second];
for (auto &it: mp) it.second = z++;
set<pair<int, int>> st;
vector<int> out(n), in(n);
for (int i = 0; i < n; i++) out[mp[adj[i].first]] = mp[adj[i].second];
// for (int i = 0; i < n; i++) cout << mp[adj[i].first] << ' ' << mp[adj[i].second] << endl;
for (int i = 0; i < n; i++) in[out[i]]++;
// for (int i = 0; i < n; i++) cout << in[i] << ' ';
// cout << endl;
for (int i = 0; i < n; i++) st.insert(make_pair(in[i], i));
vector<bool> changed(n);
int ans = 0;
while (!st.empty()) {
auto [c, u] = *st.begin(); st.erase(st.begin());
if (u == out[u] or changed[out[u]]) continue;
int v = out[u];
if (u != out[v]) {
st.erase(make_pair(in[out[v]], out[v]));
in[out[v]]--;
st.insert(make_pair(in[out[v]], out[v]));
out[v] = u;
ans++;
}
st.erase(make_pair(in[v], v));
changed[u] = changed[v] = true;
}
cout << ans + count(changed.begin(), changed.end(), false);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
195 ms |
22068 KB |
Output is correct |
5 |
Correct |
195 ms |
22080 KB |
Output is correct |
6 |
Correct |
206 ms |
22352 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
184 ms |
22100 KB |
Output is correct |
2 |
Correct |
187 ms |
22072 KB |
Output is correct |
3 |
Correct |
165 ms |
22352 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
195 ms |
22080 KB |
Output is correct |
6 |
Correct |
254 ms |
22096 KB |
Output is correct |
7 |
Correct |
190 ms |
22148 KB |
Output is correct |
8 |
Correct |
171 ms |
22248 KB |
Output is correct |
9 |
Correct |
186 ms |
22076 KB |
Output is correct |
10 |
Correct |
135 ms |
22056 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |