제출 #397395

#제출 시각아이디문제언어결과실행 시간메모리
397395KoDLove Polygon (BOI18_polygon)C++17
25 / 100
176 ms12308 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...