Submission #1083765

#TimeUsernameProblemLanguageResultExecution timeMemory
1083765IBoryLove Polygon (BOI18_polygon)C++17
100 / 100
149 ms23656 KiB
#include <bits/stdc++.h> #define pii pair<int, int> using namespace std; const int MAX = 100007; pair<string, string> E[MAX]; int F[MAX], V[MAX], in[MAX], S[MAX]; vector<int> G[MAX]; int main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; if (N & 1) { cout << -1; return 0; } map<string, int> SN; for (int i = 1; i <= N; ++i) { string a, b; cin >> a >> b; SN[a] = i; E[i] = {a, b}; } for (int i = 1; i <= N; ++i) F[SN[E[i].first]] = SN[E[i].second]; int RN = 0, match = 0; for (int i = 1; i <= N; ++i) { if (i != F[i] && F[F[i]] == i) V[i] = 1; else RN++; } for (int i = 1; i <= N; ++i) { if (V[i] || V[F[i]]) continue; G[i].push_back(F[i]); in[F[i]]++; } queue<int> Q; for (int i = 1; i <= N; ++i) if (!G[i].empty() && !in[i]) Q.push(i); while (!Q.empty()) { int cur = Q.front(); Q.pop(); if (V[F[cur]]) continue; if (!S[cur] && !S[F[cur]]) { match++; S[cur] = S[F[cur]] = 1; } if (!--in[F[cur]]) Q.push(F[cur]); } for (int i = 1; i <= N; ++i) { if (!in[i]) continue; int cur = i; vector<int> C = {S[cur]}; while (1) { cur = F[cur]; in[cur]--; if (cur == i) break; C.push_back(S[cur]); } bool one = (*max_element(C.begin(), C.end()) == 1); if (!one) match += C.size() / 2; else { int piv = max_element(C.begin(), C.end()) - C.begin(); for (int i = 0; i < C.size(); ++i) { int a = (piv + i) % C.size(), b = (piv + i + 1) % C.size(); if (!C[a] && !C[b]) { match++; C[a] = C[b] = 1; } } } } int ans = RN - match; cout << ans; return 0; }

Compilation message (stderr)

polygon.cpp: In function 'int main()':
polygon.cpp:68:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |    for (int i = 0; i < C.size(); ++i) {
      |                    ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...