Submission #988608

#TimeUsernameProblemLanguageResultExecution timeMemory
988608huutuanParachute rings (IOI12_rings)C++14
0 / 100
800 ms60436 KiB
#include <bits/stdc++.h> using namespace std; struct DSU{ int n, cnt, idx; vector<int> lab, cyc; void init(int _n){ n=_n; cnt=0; lab.assign(n+1, -1); cyc.assign(n+1, 0); idx=-1; } int find_set(int u){ return lab[u]<0?u:lab[u]=find_set(lab[u]); } void union_sets(int u, int v){ u=find_set(u); v=find_set(v); if (u!=v){ if (lab[u]>lab[v]) swap(u, v); lab[u]+=lab[v]; lab[v]=u; cnt-=cyc[u]; cnt-=cyc[v]; cnt+=(cyc[u]+=cyc[v]); cyc[v]=0; }else cnt-=cyc[u], cnt+=(cyc[u]+=1); if (cyc[u]) idx=u; } } dsu; const int N=1e6+10; int deg[N]; int n; set<int> st3, st4; set<int> adj; int cnt3; vector<int> g[N], cycle; int vis[N], par[N], chk[N]; void Init(int N_) { n=N_; dsu.init(n); } void dfs(int u, int p){ vis[u]=1; par[u]=p; for (int v:g[u]) if (v!=p){ if (!vis[v]) dfs(v, u); else if (cycle.empty()){ int w=u; while (w!=v) cycle.push_back(w), w=par[w]; cycle.push_back(v); for (int x:cycle) chk[x]=1; } } } void Link(int u, int v) { ++u; ++v; dsu.union_sets(u, v); g[u].push_back(v); g[v].push_back(u); if (dsu.cnt && cycle.empty()){ dfs(dsu.idx, 0); } if (deg[u]==3 && (int)st4.size()==1 && adj.count(u)) --cnt3; if (deg[v]==3 && (int)st4.size()==1 && adj.count(v)) --cnt3; st3.erase(u); st3.erase(v); ++deg[u]; ++deg[v]; if (deg[u]==3) st3.insert(u); if (deg[v]==3) st3.insert(v); if (deg[u]>3) st4.insert(u); if (deg[v]>3) st4.insert(v); if (deg[u]<deg[v]) swap(u, v); if ((int)st4.size()==1 && *st4.begin()==u){ adj.insert(v); if (deg[v]==3) ++cnt3; } if (deg[u]==3 && (int)st4.size()==1 && adj.count(u)) ++cnt3; if (deg[v]==3 && (int)st4.size()==1 && adj.count(v)) ++cnt3; } int CountCritical() { if (st3.empty() && st4.empty()){ if (dsu.cnt==0) return n; if (dsu.cnt==1) return -dsu.lab[dsu.idx]; return 0; } if ((int)st4.size()>=2) return 0; if ((int)st4.size()==1){ return cnt3==(int)st3.size() && dsu.cnt==1 && chk[*st4.begin()]; } if ((int)st3.size()>=5) return 0; if ((int)st3.size()==2 && dsu.cnt==2){ int u=*st3.begin(), v=*st3.rbegin(); bool check=1, check2=0; set<int> st; for (int w:g[u]) if (w!=v) st.insert(w); else check2|=1; check&=check2; check&=(int)st.size()==2; for (int w:g[v]) if (w!=u) st.insert(w); check&=(int)st.size()==2; return check; } int ans=0; for (int u:st3){ int cnt=0; for (int v:g[u]) cnt+=deg[v]==3; if (cnt==(int)st3.size()-1 && (!dsu.cnt || (dsu.cnt==1 && chk[u]))) ++ans; } if ((int)st3.size()==1){ for (int u:g[*st3.begin()]) ans+=!dsu.cnt || (dsu.cnt==1 && chk[u]); } return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...