#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 |
1 |
Incorrect |
1 ms |
208 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 |
176 ms |
12236 KB |
Output is correct |
5 |
Correct |
172 ms |
12100 KB |
Output is correct |
6 |
Correct |
176 ms |
12308 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
176 ms |
12188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |