Submission #873350

#TimeUsernameProblemLanguageResultExecution timeMemory
873350Yahia_EmaraLogičari (COCI21_logicari)C++17
110 / 110
156 ms44052 KiB
#include <bits/stdc++.h> #define pb push_back #define ctoi(x) int(x-'0') #define cdv(x,y) (((x)+(y)-1)/(y)) #define LOOP(n) for(int rp=0;rp<(n);rp++) #define sz(x) int(x.size()) #define dbg(x) cout << (#x) << " : " << x << endl; #define sq(x) ((x)*(x)) using namespace std; typedef long long ll; typedef long double dl; const int SZ=2e5+7; int n,dp[SZ][2][2][2][2],X,Y; bool vs[SZ]; vector<int>G[SZ],g[SZ]; void dfs(int x,int pr=-1){ vs[x]=1; for(auto&i:G[x]){ if(i==pr)continue; if(vs[i])X=x,Y=i; else dfs(i,x),g[x].pb(i),g[i].pb(x); } } int solve(int nd,int pr,int b,int c,int u,int e){ if(nd==Y){ if(e+b!=1)return 1e9; if(c+u>1)return 1e9; c+=u; } if(pr!=-1&&sz(g[nd])==1){ if(c==0)return 1e9; return b; } if(dp[nd][b][c][u][e]!=-1)return dp[nd][b][c][u][e]; ll s=0,mn=2e9; for(auto&i:g[nd]){ if(i==pr)continue; s+=ll(solve(i,nd,0,b,u,e)); mn=min(mn,ll(solve(i,nd,1,b,u,e)-solve(i,nd,0,b,u,e))); } ll s2=1e9; if(nd==X){ s2=0; for(auto&i:g[nd]){ if(i==pr)continue; s2+=ll(solve(i,nd,0,b,u,0)); } } return dp[nd][b][c][u][e]=min(ll(1e9),min(c?s:s+mn,s2)+b); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int tt=1; //cin >> tt; LOOP(tt){ cin >> n; memset(dp,-1,sizeof(dp)); for(int i=0;i<n;i++){ int x,y;cin >> x >> y; G[x].pb(y),G[y].pb(x); } dfs(1); int ans=min(solve(X,-1,0,0,0,1),solve(X,-1,1,0,1,1)); if(ans==1e9)ans=-1; cout << ans; } 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...