Submission #442531

#TimeUsernameProblemLanguageResultExecution timeMemory
442531abc864197532Love Polygon (BOI18_polygon)C++17
29 / 100
320 ms37168 KiB
#include <bits/stdc++.h> using namespace std; #define lli long long int #define X first #define Y second #define pb push_back #define eb emplace_back #define mp make_pair #define pii pair<int, int> #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define test(args...) abc("[" + string(#args) + "]", args) void abc() {cerr << endl;} template <typename T, typename ...U> void abc(T a, U ...b) { cerr << a << ' ', abc(b...); } template <typename T> void printv(T l, T r) { while (l != r) cerr << *l << " \n"[++l == r]; } const int mod = 1e9 + 7, N = 200000; int dsu[N]; int Find(int x) { if (x == dsu[x]) return x; return dsu[x] = Find(dsu[x]); } bool Union(int u, int v) { u = Find(u), v = Find(v); if (u == v) return false; dsu[u] = v; return true; } int main () { ios::sync_with_stdio(false); cin.tie(0); int n, _id = 0; cin >> n; if (n & 1) return cout << -1 << endl, 0; map <string, int> m1; auto get = [&](string s) { if (m1.count(s)) return m1[s]; return m1[s] = _id++; }; for (int i = 0; i < n; ++i) dsu[i] = i; vector <vector <int>> adj(n); string s, t; vector <pii> oth; for (int i = 0; i < n; ++i) { cin >> s >> t; int u = get(s), v = get(t); if (Union(u, v)) adj[v].pb(u), adj[u].pb(v); else oth.eb(v, u); } vector <int> cur; vector <vector <int>> dp1(n, vector <int>(2, 0)); vector <vector <int>> dp2(n, vector <int>(2, 0)); // 0 not choose, 1 choose vector <bool> vis(n, false); int cnt = 0; function<void(int, int, int)> dfs = [&](int v, int pa, int out) { cnt++; for (int u : adj[v]) if (u != pa) { dfs(u, v, out); dp1[v][0] += max(dp1[u][0], dp1[u][1]); dp2[v][0] += max(dp2[u][0], dp2[u][1]); } for (int u : adj[v]) if (u != pa) { dp1[v][1] = max(dp1[v][1], dp1[v][0] - max(dp1[u][0], dp1[u][1]) + dp1[u][0] + 1); dp2[v][1] = max(dp2[v][1], dp2[v][0] - max(dp2[u][0], dp2[u][1]) + dp2[u][0] + 1); } if (v == out) dp2[v][1] = -1 << 30; }; int ans = n; for (pii A : oth) { cnt = 0; dfs(A.X, -1, A.Y); if (A.X != A.Y) ans -= max({dp1[A.X][0], dp1[A.X][1], dp2[A.X][0] + 1}); else ans -= max(dp1[A.X][0], dp1[A.X][1]); if (A.X != A.Y && cnt == 2) ans--; } cout << ans << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...