Submission #716426

#TimeUsernameProblemLanguageResultExecution timeMemory
716426parsadox2Love Polygon (BOI18_polygon)C++14
100 / 100
258 ms31164 KiB
#include <bits/stdc++.h> #define pb push_back #define F first #define S second #define debug(x) cout << #x << "= " << x << ", " #define ll long long #define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define SZ(x) (int) x.size() #define wall cout << endl; using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int maxn = 1e5 + 10; int n , ar[maxn] , dp[maxn][2] , super_man; map <string , int> mp; vector <pair<string , string>> edges; vector <int> adj[maxn] , trav; bool vis[maxn] , marked[maxn]; int Find_root(int v) { vis[v] = true; if(vis[ar[v]]) return v; return Find_root(ar[v]); } void dfs(int v) { marked[v] = true; bool flg = false; for(auto u : adj[v]) if(!marked[u]) { dfs(u); dp[v][0] += dp[u][1]; dp[v][1] += dp[u][1]; if(dp[u][1] == dp[u][0]) flg = true; } dp[v][1] += flg; } void dfs2(int v , int root) { bool flg = false; dp[v][0] = dp[v][1] = 0; for(auto u : adj[v]) if(u != root) { dfs2(u , root); dp[v][0] += dp[u][1]; dp[v][1] += dp[u][1]; if(dp[u][1] == dp[u][0] && u != super_man) flg = true; } dp[v][1] += flg; if(v == super_man) dp[v][1] = dp[v][0]; } int solve(int root) { dfs(root); int res = dp[root][1]; if(ar[root] == root) return res; super_man = ar[root]; dfs2(root , root); int tmp = (ar[ar[root]] == root ? 2 : 1); res = max(res , dp[root][0] + tmp); return res; } int32_t main() { fast; cin >> n; for(int i = 0 ; i < n ; i++) { string a , b; cin >> a >> b; edges.pb({a , b}); mp[a] = i; } if(n & 1) { cout << -1 << endl; return 0; } for(auto u : edges) { int s = mp[u.F] , e = mp[u.S]; ar[s] = e; adj[e].pb(s); } int ans = n; for(int i = 0 ; i < n ; i++) if(!marked[i]) { int r = Find_root(i); ans -= solve(r); } cout << ans << endl; 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...