Submission #1249184

#TimeUsernameProblemLanguageResultExecution timeMemory
1249184al95ireyizLogičari (COCI21_logicari)C++20
20 / 110
1094 ms5704 KiB
//*** Bismillah ***// #pragma GCC optimize("O3", "fast-math", "unroll-loops", "no-stack-protector") #include <bits/stdc++.h> using namespace std; #if !defined(ONLINE_JUDGE) and !defined(EVAL) #include "template/debug.h" #else #define d(x...) #endif #define fr first #define er erase #define sc second #define in insert #define ll long long #define pb push_back #define vll vector<ll> #define pll pair<ll,ll> #define ull unsigned ll #define vpll vector<pll> #define len(x) (ll) x.size() #define all(x) x.begin(),x.end() const ll INF = 1e9; const ll INFL = 1e18; const ll MOD = 1e9+7; // const ll MOD = 998244353; const ll maxn = 1e5+5; ll n,m,k=0; vll g[maxn]; bool vis[maxn]; ll spec, root, dp[maxn][2][2][2][2]; void dfs(ll u, ll p = -1){ vis[u] = 1; for(auto v : g[u]){ if(v == p) continue; if(vis[v]){ root = v; spec = u; return; } dfs(v, u); if(root != 0) return; } } #define dpp dp[u][c][up][rt][sp] ll get(ll u, ll c, ll up, ll rt, ll sp, ll p = -1){ if(u == spec and c != sp) return dpp = INF; if(u == root and c != rt) return dpp = INF; if(up == 1 and u == root and sp) return dpp = INF; if(up == 1 and u == spec and rt) return dpp = INF; if(dpp != INF) return dpp; ll cm = c; for(auto v : g[u]){ if(v == p) continue; cm += get(v, 0, c, rt, sp, u); } if(up or (u == root and sp) or (u == spec and rt)){ return dpp = cm; } else{ ll cv = INF; for(auto v : g[u]){ if(v == p) continue; cv = min(cv, cm - get(v, 0, c, rt, sp, u) + get(v, 1, c, rt, sp, u)); } return dpp = cv; } } void _(ll &tt){ cin >> n; for(ll i = 1, a, b; i <= n; i ++){ cin >> a >> b; g[a].pb(b); g[b].pb(a); } ll leaf = -1; for(ll i = 1; i <= n; i ++) if(len(g[i]) == 1){ leaf = i; break; } if(leaf == -1){ cout << (n % 4 == 0 ? n / 2 : -1) << '\n'; return; } dfs(1); g[root].erase(find(all(g[root]), spec)); g[spec].erase(find(all(g[spec]), root)); d(spec, root); for(ll i = 1; i <= n; i ++){ for(auto j : {0, 1}) for(auto q : {0, 1}) for(auto l : {0, 1}) for(auto p : {0, 1}){ dp[i][j][q][l][p] = INF; // yea, ik } } ll cv = INF; for(auto i : {0, 1}) for(auto j : {0, 1}){ cv = min(cv, get(root, i, 0, i, j)); } cout << (cv == INF ? -1 : cv) << '\n'; } signed main() { ll tm = clock(); cin.tie(0)->sync_with_stdio(0); ll t = 1; // cin >> t; for(ll tt = 1; tt <= t; tt ++) { _(tt); } cerr << "\n\033[1;31mTime: \033[1;30m" \ << (double)(clock()-tm)/1000000 << "\033[1;32m seconds\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...