Submission #924367

#TimeUsernameProblemLanguageResultExecution timeMemory
924367aykhnLove Polygon (BOI18_polygon)C++17
29 / 100
155 ms24400 KiB
#include <bits/stdc++.h> // author: aykhn using namespace std; #define int long long #define inf 0x3F3F3F3F const int MXN = 1e5 + 5; int n; vector<int> adj[MXN]; map<string, int> mp; int to[MXN], dp[MXN][2]; void dfs(int a) { if (adj[a].empty()) { dp[a][0] = dp[a][1] = 1; return; } dp[a][0] = 0; dp[a][1] = inf; for (int v : adj[a]) { dfs(v); dp[a][0] += min(dp[v][0], dp[v][1]); } for (int v : adj[a]) { dp[a][1] = min(dp[a][1], dp[a][0] - min(dp[v][0], dp[v][1]) + dp[v][0] - 1); } dp[a][0]++; assert(dp[a][1] <= dp[a][0]); } signed main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n, m = 0; cin >> n; if (n & 1) { cout << -1 << '\n'; return 0; } for (int i = 1; i <= n; i++) { string a, b; cin >> a >> b; if (mp.find(a) == mp.end()) mp[a] = ++m; if (mp.find(b) == mp.end()) mp[b] = ++m; to[mp[a]] = mp[b]; } vector<int> r; for (int i = 1; i <= m; i++) { if (to[i] == i) r.push_back(i); if (to[to[i]] == i) continue; adj[to[i]].push_back(i); } int res = 0; for (int i : r) { dfs(i); res += min(dp[i][0], dp[i][1]); } cout << (n + res)/2 << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...