#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;
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 || chk[u])) ++ans;
}
if ((int)st3.size()==1){
for (int u:g[*st3.begin()]) ans+=!dsu.cnt || chk[u];
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
29100 KB |
Output is correct |
2 |
Incorrect |
8 ms |
29360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
356 ms |
56168 KB |
Output is correct |
2 |
Incorrect |
755 ms |
71252 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
29100 KB |
Output is correct |
2 |
Incorrect |
8 ms |
29360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
29100 KB |
Output is correct |
2 |
Incorrect |
8 ms |
29360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
29100 KB |
Output is correct |
2 |
Incorrect |
8 ms |
29360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |