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>
template <class T>
using Vec = std::vector<T>;
int main() {
int N;
std::cin >> N;
if (N % 2 == 1) {
std::cout << -1 << '\n';
return 0;
}
Vec<int> to(N);
{
Vec<std::string> U(N), V(N);
for (int i = 0; i < N; ++i) {
std::cin >> U[i] >> V[i];
}
auto cmp = U;
std::sort(cmp.begin(), cmp.end());
for (int i = 0; i < N; ++i) {
const auto u = std::lower_bound(cmp.cbegin(), cmp.cend(), U[i]) - cmp.cbegin();
const auto v = std::lower_bound(cmp.cbegin(), cmp.cend(), V[i]) - cmp.cbegin();
to[u] = v;
}
}
int use = 0;
Vec<bool> used(N);
for (int i = 0; i < N; ++i) {
if (!used[i]) {
if (to[i] == i) {
used[i] = true;
}
else if (to[to[i]] == i) {
use += 1;
used[i] = true;
}
}
}
for (int i = 0; i < N; ++i) {
if (!used[i]) {
int cnt = 0;
int u = i;
while (!used[u]) {
cnt += 1;
used[u] = true;
u = to[u];
}
use += cnt / 2;
}
}
std::cout << N - use << '\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... |