Submission #780164

#TimeUsernameProblemLanguageResultExecution timeMemory
780164cig32Logičari (COCI21_logicari)C++17
110 / 110
142 ms72916 KiB
#include "bits/stdc++.h" using namespace std; #define int long long const int MAXN = 1e6 + 10; const int MOD = 998244353; mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x, int y) { int u = uniform_int_distribution<int>(x, y)(rng); return u; } int bm(int b, int p) { if(p==0) return 1 % MOD; int r = bm(b, p >> 1); if(p&1) return (((r*r) % MOD) * b) % MOD; return (r*r) % MOD; } int inv(int b) { return bm(b, MOD-2); } int fastlog(int x) { return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1); } void printcase(int i) { cout << "Case #" << i << ": "; } int n; vector<int> adj[MAXN]; vector<int> cycle; vector<int> dhs[MAXN]; bool vis[MAXN]; int par[MAXN]; int dep[MAXN]; void dfs0(int node, int prv) { vis[node] = 1; dep[node] = (prv == -1 ? 0 : dep[prv] + 1); par[node] = prv; for(int x: dhs[node]) { if(!vis[x]) { dfs0(x, node); } else if(x != prv && prv != -1) { cycle.clear(); int u = node, v = x; if(dep[u] > dep[v]) swap(u, v); while(u != v) { cycle.push_back(v); v = par[v]; } cycle.push_back(u); } } } bool constraint[MAXN];//must be white int dp[MAXN][2][2]; void dfs(int node, int prv) { vector<int>child; for(int x: adj[node]) { if(x != prv) { dfs(x, node); child.push_back(x); } } // dp[node][0][0] dp[node][0][0]=0; for(int x:child)dp[node][0][0]+=dp[x][0][1]; dp[node][0][0]=min(dp[node][0][0],(int)(1e9)); // dp[node][0][1] int sm=0; for(int x:child)sm+=dp[x][0][1]; dp[node][0][1]=1e9; for(int x:child){ int nw=sm-dp[x][0][1]+dp[x][1][1]; dp[node][0][1]=min(dp[node][0][1],nw); } // dp[node][1][0] dp[node][1][0]=1; for(int x:child)dp[node][1][0]+=dp[x][0][0]; dp[node][1][0]=min(dp[node][1][0],(int)(1e9)); // dp[node][1][1] sm=0; for(int x:child)sm+=dp[x][0][0]; dp[node][1][1]=1e9; for(int x:child){ int nw=sm-dp[x][0][0]+dp[x][1][0]; dp[node][1][1]=min(dp[node][1][1],nw+1); } if(constraint[node]==1) dp[node][1][0]=dp[node][1][1]=1e9; } void solve(int tc){ cin >> n; vector<pair<int,int>>edges; for(int i=1; i<=n; i++) { int u, v; cin >> u >> v; edges.push_back({u,v}); dhs[u].push_back(v); dhs[v].push_back(u); } dfs0(1, -1); vector<pair<int,int>>banned; int len=cycle.size(); for(int i=0;i<min(len,4ll);i++) banned.push_back({cycle[i],cycle[(i+1)%len]}); int ans = 1e9; for(pair<int,int>x:banned){ for(int i=1; i<=n; i++) { constraint[i] = 0; adj[i].clear(); for(int j=0; j<2; j++) { for(int k=0; k<2; k++) { dp[i][j][k] = 1e9; } } } constraint[x.first] = constraint[x.second] = 1; pair<int,int>alt=x; swap(alt.first,alt.second); for(pair<int,int>y:edges){ if(x==y || alt==y) continue; adj[y.first].push_back(y.second); adj[y.second].push_back(y.first); } dfs(1, -1); ans = min(ans, dp[1][0][1]); ans = min(ans, dp[1][1][1]); } cout << (ans == 1e9 ? -1 : ans) << '\n'; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; for(int i=1; i<=t; i++) solve(i); } // 勿忘初衷
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...