Submission #987777

#TimeUsernameProblemLanguageResultExecution timeMemory
987777MilosMilutinovicLove Polygon (BOI18_polygon)C++14
0 / 100
350 ms21072 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> to(n); map<string, int> mp; for (int i = 0; i < n; i++) { string s; cin >> s; string t; cin >> t; if (!mp.count(s)) { mp[s] = (int) mp.size(); } if (!mp.count(t)) { mp[t] = (int) mp.size(); } to[mp[s]] = mp[t]; } if (n % 2 == 1) { cout << -1 << '\n'; return 0; } vector<vector<int>> ch(n); for (int i = 0; i < n; i++) { if (to[i] == i) { continue; } ch[to[i]].push_back(i); } vector<int> deg(n); for (int i = 0; i < n; i++) { deg[to[i]] += 1; } vector<int> que; for (int i = 0; i < n; i++) { if (deg[i] == 0) { que.push_back(i); } } for (int b = 0; b < (int) que.size(); b++) { int i = que[b]; deg[to[i]] -= 1; if (deg[to[i]] == 0) { que.push_back(to[i]); } } vector<vector<int>> dp(n, vector<int>(2)); function<void(int)> Solve = [&](int v) { int mx = 0; for (int u : ch[v]) { Solve(u); dp[v][0] += max(dp[u][0], dp[u][1]); dp[v][1] += dp[u][0]; mx = max(mx, -dp[u][0] + dp[u][1] + 1); } dp[v][1] += mx; }; int res = 0; for (int b = 0; b < (int) que.size(); b++) { int i = que[b]; Solve(i); res += dp[i][1]; } cout << (n - 2 * res) + res << '\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...