Submission #1301079

#TimeUsernameProblemLanguageResultExecution timeMemory
1301079trandangquangLogičari (COCI21_logicari)C++20
10 / 110
71 ms26200 KiB
#include<bits/stdc++.h> using namespace std; #define foru(i,a,b) for(int i=(a); i<=(b); ++i) #define ford(i,a,b) for(int i=(a); i>=(b); --i) #define rep(i,a) for(int i=0; i<(a); ++i) #define sz(a) (int)(a).size() #define all(a) (a).begin(),(a).end() #define bit(s,i) (((s)>>(i))&1) #define ii pair<int,int> #define vi vector<int> #define vii vector<ii> #define fi first #define se second #define ll long long #define eb emplace_back #define pb push_back #define __builtin_popcount __builtin_popcountll #define _ << " " << template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;} template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;} const int N=1e5+5; const int INF=1e9; int n; vi adj[N]; void input(){ cin>>n; foru(i,1,n){ int u,v; cin>>u>>v; adj[u].eb(v); adj[v].eb(u); } } int trace[N],vis[N]; bool inCycle[N]; vi cycle; void dfs1(int u, int p=-1){ vis[u]=1; for(int v:adj[u]) if(v!=p){ if(sz(cycle)) return; if(vis[v]){ int t=u; while(t!=v){ cycle.eb(t); t=trace[t]; } cycle.eb(v); } else{ trace[v]=u; dfs1(v,u); } } } void findCycle(){ dfs1(1); for(int i:cycle) inCycle[i]=1; } int dp1[N][2][2]; /// blue-eye?, is-seen? int f[2][2]; void dfs2(int u, int p=-1){ dp1[u][0][0]=0; dp1[u][1][0]=1; for(int v:adj[u]) if(v!=p && !inCycle[v]){ dfs2(v,u); f[0][1]=min(dp1[u][0][0]+dp1[v][1][1], dp1[u][0][1]+dp1[v][0][1]); f[0][0]=dp1[u][0][0]+dp1[v][0][1]; f[1][1]=min(dp1[u][1][0]+dp1[v][1][0], dp1[u][1][1]+dp1[v][0][0]); f[1][0]=dp1[u][1][0]+dp1[v][0][0]; rep(i,2) rep(j,2) dp1[u][i][j]=f[i][j]; } } void dpTree(){ memset(dp1,0x3f,sizeof(dp1)); for(int i:cycle) dfs2(i); } int dp2[N][2][2][2][2];///status of 0, status of cycle[i] void dpCycle(){ memset(dp2,0x3f,sizeof(dp2)); rep(i,2) rep(j,2) dp2[cycle[0]][i][j][i][j]=dp1[cycle[0]][i][j]; rep(i,sz(cycle)-1) rep(h1,2) rep(h2,2) rep(j,2) rep(k,2){ /// ta co trang thai cua i /// chuyen sang trang thai cua i+1 nhu the nao ??? /// dau tien for trang thai cua i+1 /// neu trang thai i+1 dam trang thai cua i -> fail /// neu ko -> cong vao rep(nj,2) rep(nk,2){ if(k && nj) continue; if(j && nk) continue; if(i!=0 && !k && !nj) continue; if(i==0){ mini(dp2[cycle[i+1]][h1][h2|nj][nj][nk|j],dp2[cycle[i]][h1][h2][j][k]+dp1[cycle[i+1]][nj][nk]); } else{ mini(dp2[cycle[i+1]][h1][h2][nj][nk|j],dp2[cycle[i]][h1][h2][j][k]+dp1[cycle[i+1]][nj][nk]); } } } int res=INF; rep(j1,2) rep(k1,2) rep(j2,2) rep(k2,2){ if(k1 && j2) continue; if(k2 && j1) continue; if(!k2 && !j1) continue; if(!k1 && !j2) continue; mini(res, dp2[cycle.back()][j1][k1][j2][k2]); } cout<<(res==INF?-1:res)<<'\n'; } void solve(){ input(); findCycle(); dpTree(); dpCycle(); } int32_t main(){ #define task "test" if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } cin.tie(0)->sync_with_stdio(0); int tc=1; //cin>>tc; foru(i,1,tc){ solve(); } }

Compilation message (stderr)

Main.cpp: In function 'int32_t main()':
Main.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...