This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int id = 0;
map<string, int> mp;
vector<int> to(n * 2);
vector<int> deg(n * 2);
for (int i = 0; i < n; ++i) {
string s1, s2;
cin >> s1 >> s2;
if (!mp.count(s1)) {
mp[s1] = id++;
}
if (!mp.count(s2)) {
mp[s2] = id++;
}
if (s1 != s2) {
deg[mp[s2]]++;
}
to[mp[s1]] = mp[s2];
}
if (n % 2) {
cout << -1 << '\n';
return 0;
}
vector<bool> vis(id);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int i = 0; i < id; ++i) {
if (to[i] != i && to[to[i]] == i) {
vis[i] = true;
} else {
pq.emplace(deg[i], i);
}
}
vector<bool> seen(n);
int ans = 0;
while (!pq.empty()) {
auto [d, v] = pq.top();
pq.pop();
if (seen[v]) {
continue;
}
seen[v] = true;
if (to[v] == v) {
ans++;
continue;
}
deg[to[v]]--;
pq.emplace(0, to[v]);
if (!vis[v]) {
ans++;
vis[to[v]] = true;
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |