Submission #673384

#TimeUsernameProblemLanguageResultExecution timeMemory
673384HalfLogičari (COCI21_logicari)C++17
110 / 110
163 ms19484 KiB
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; #define REP(i,a,b) for(ll i=(ll) a; i<(ll) b; i++) #define pb push_back #define mp make_pair #define pl pair<ll,ll> #define ff first #define ss second #define whole(x) x.begin(),x.end() #define DEBUG(i) cout<<"WAFFLES "<<i<<"<\n" #define INF 10000000LL #define EPS (0.00000000001L) #define pi (3.141592653589793L) #define VV(vvvv,NNNN,xxxx); REP(iiiii,0,NNNN) {vvvv.pb(xxxx);} ll mod=1000000007LL; template<class A=ll> void Out(vector<A> a) {REP(i,0,a.size()) {cout<<a[i]<<" ";} cout<<endl;} template<class A=ll> void In(vector<A> &a, ll N) {A cur; REP(i,0,N) {cin>>cur; a.pb(cur);}} vector<vector<ll>> adj; ll n; vector<bool> vis; pair<ll, ll> cyc; pair<ll,ll> dfs(ll prv, ll nd){ vis[nd] = true; pair<ll, ll> sol = {-1, -1}; for(ll nb : adj[nd]){ if(nb == prv) continue; if(vis[nb]){ return {nd,nb}; } sol = max(sol, dfs(nd, nb)); if(sol.ff > -1) break; } return sol; } bool root_seeing, root_blue, cyc_blue; vector<array<array<ll,2>,2>> mem; ll tree_dfs(ll pr, ll nd, bool seeing, bool blue){ if(mem[nd][seeing][blue] != -1) return mem[nd][seeing][blue]; if(nd == cyc.ss){ if(blue != root_seeing){ mem[nd][seeing][blue] = INF; return INF; } if(seeing && root_blue){ mem[nd][seeing][blue] = INF; return INF; } if(adj[nd].size() == 2){ if(seeing || root_blue){ mem[nd][seeing][blue] = blue; return blue; }else{ mem[nd][seeing][blue] = INF; return INF; } } if(root_blue) seeing = true; } if(adj[nd].size() == 1){ if(seeing){ mem[nd][seeing][blue] = blue; return blue; }else{ mem[nd][seeing][blue] = INF; return INF; } } if(seeing){ if(blue){ ll sum = 0; for(ll ch : adj[nd]){ if(ch == pr) continue; if(ch == cyc.ff) continue; sum += tree_dfs(nd, ch, true, false); } mem[nd][seeing][blue] = sum+1; return sum + 1; }else{ ll sum = 0; for(ll ch : adj[nd]){ if(ch == pr) continue; if(ch == cyc.ff) continue; sum += tree_dfs(nd, ch, false, false); } mem[nd][seeing][blue] = sum; return sum; } }else{ if(blue){ ll sum = 0; ll mndf = INF; for(ll ch : adj[nd]){ if(ch == pr) continue; if(ch == cyc.ff) continue; ll nb_ntb = tree_dfs(nd, ch, true, false); ll nb_b = tree_dfs(nd, ch, true, true); sum += nb_ntb; mndf = min(mndf, nb_b-nb_ntb); } mem[nd][seeing][blue] = sum + mndf + 1; return sum + mndf + 1; }else{ ll sum = 0; ll mndf = INF; for(ll ch : adj[nd]){ if(ch == pr) continue; if(ch == cyc.ff) continue; ll nb_ntb = tree_dfs(nd, ch, false, false); ll nb_b = tree_dfs(nd, ch, false, true); sum += nb_ntb; mndf = min(mndf, nb_b-nb_ntb); } mem[nd][seeing][blue] = sum + mndf; return sum + mndf; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(20); cin >> n; adj.assign(n,{}); ll a, b; for(ll i = 0; i < n; ++i){ cin >> a >> b; a--;b--; adj[a].pb(b); adj[b].pb(a); } vis.assign(n, 0); cyc = dfs(-1, 0); mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}}); ll mn = INF; root_seeing = false; root_blue = false; mn = min(mn, tree_dfs(cyc.ss, cyc.ff, false, false)); mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}}); root_seeing = false; root_blue = true; mn = min(mn, tree_dfs(cyc.ss, cyc.ff, false, true)); mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}}); root_seeing = true; root_blue = false; mn = min(mn, tree_dfs(cyc.ss, cyc.ff, true, false)); mem.assign(n, {array<ll,2>{-1,-1},array<ll,2>{-1,-1}}); root_seeing = true; root_blue = true; mn = min(mn, tree_dfs(cyc.ss, cyc.ff, true, true)); if(mn >= INF){ cout << "-1\n"; }else{ cout << mn << "\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...