Submission #939421

#TimeUsernameProblemLanguageResultExecution timeMemory
939421Gromp15Love Polygon (BOI18_polygon)C++17
25 / 100
295 ms19916 KiB
#include <bits/stdc++.h> #define ll long long #define ar array #define db double #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define rint(l, r) uniform_int_distribution<int>(l, r)(rng) template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } void test_case() { int n; cin >> n; map<string, int> mp; vector<int> a(n); vector<vector<int>> adj2(n); vector<int> indeg(n); for (int i = 0; i < n; i++) { string s, t; cin >> s >> t; if (!mp.count(s)) mp[s] = mp.size(); if (!mp.count(t)) mp[t] = mp.size(); a[mp[s]] = mp[t]; adj2[mp[s]].push_back(mp[t]); adj2[mp[t]].push_back(mp[s]); indeg[mp[t]]++; } if (n & 1) { cout << -1 << '\n'; return; } vector<bool> vis(n), dir(n); int ans = 0, extra = 0; for (int i = 0; i < n; i++) if (!vis[i]) { vector<int> got; queue<int> q; auto dfs = [&](auto&& s, int v) -> void { got.emplace_back(v); vis[v] = 1; if (!indeg[v]) q.push(v); for (int u : adj2[v]) if (!vis[u]) s(s, u); }; dfs(dfs, i); while (q.size()) { int v = q.front(); q.pop(); if (dir[v]) continue; int nxt = a[v]; if (!dir[nxt]) { dir[nxt] = dir[v] = 1; ans++; } else { dir[v] = 1; extra++; // we need to find someone else } if (--indeg[nxt] == 0) q.push(nxt); } int ccl = 0; for (int v : got) if (!dir[v]) { ccl++; } for (int v : got) if (!dir[v]) { int nxt = a[v]; if (v == nxt) { dir[v] = 1; extra++; continue; } if (!dir[nxt]) { dir[nxt] = dir[v] = 1; ans += a[nxt] != v; } else { dir[v] = 1; extra++; } } } cout << ans + extra << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); int t = 1; // cin >> t; while (t--) test_case(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...