//#include "grader.cpp"
#include <set>
#include <map>
#include <vector>
#include <cstring>
#include <assert.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int MAXN = 1e6 + 5;
struct Graph
{
struct DSU
{
int n;
int deg[MAXN];
int parent[MAXN], treeSize[MAXN];
bool isBad[MAXN];
int badComponentSz;
int components, badComponents;
DSU(){}
DSU(int n)
{
this->n = n;
this->components = n;
this->badComponents = 0;
for(int i = 0;i<n;i++)
{
this->deg[i] = 0;
this->parent[i] = i;
this->treeSize[i] = 1;
this->isBad[i] = false;
}
}
int Find(int x)
{
if(parent[x]==x) return x;
parent[x] = Find(parent[x]);
return parent[x];
}
void Union(int u, int v)
{
int uOrig = u, vOrig = v;
u = Find(u);
v = Find(v);
badComponents -= isBad[u];
badComponents -= isBad[v];
isBad[u] |= isBad[v];
if(deg[uOrig]>1 || deg[vOrig]>1) isBad[u] = true;
parent[v] = u;
treeSize[u] += treeSize[v];
badComponents += isBad[u];
if(isBad[u]==true) badComponentSz = treeSize[u];
}
void addEdge(int u, int v)
{
if(Find(u)!=Find(v))
{
Union(u, v);
}
else
{
if(isBad[Find(u)]==false)
{
isBad[Find(u)] = true;
badComponents++;
badComponentSz = treeSize[Find(u)];
}
}
deg[u]++;
deg[v]++;
}
};
int n;
DSU T;
int maxDeg = 0;
vector <int> adj[MAXN];
vector <pair <int, int>> allEdges;
int superDegCnt = 0;
Graph(){}
Graph(int n)
{
this->n = n;
T = DSU(n);
}
void addEdge(int u, int v, bool mode = false)
{
T.addEdge(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
if(mode==true) allEdges.push_back({u, v});
maxDeg = max(maxDeg, int(adj[u].size()));
maxDeg = max(maxDeg, int(adj[v].size()));
if(adj[u].size()==4) superDegCnt++;
if(adj[v].size()==4) superDegCnt++;
}
};
Graph G;
map <int, Graph> excludedNode;
void Init(int N)
{
G = Graph(N);
}
void initFirst()
{
int node = -1;
for(int x = 0;x<G.n;x++)
{
if(G.adj[x].size()==3)
{
node = x;
break;
}
}
excludedNode[node] = Graph(G.n);
for(int x: G.adj[node]) excludedNode[x] = Graph(G.n);
for(auto it = excludedNode.begin();it!=excludedNode.end();it++)
{
int x = it->first;
for(pair <int, int> e: G.allEdges)
{
if(e.first!=x && e.second!=x)
{
it->second.addEdge(e.first, e.second);
}
}
}
}
bool initSecondCalled = false;
void initSecond()
{
initSecondCalled = true;
int node = -1;
for(int x = 0;x<G.n;x++)
{
if(G.adj[x].size()>=4)
{
node = x;
break;
}
}
if(excludedNode.count(node)==false)
{
excludedNode[node] = Graph(G.n);
for(pair <int, int> e: G.allEdges)
{
if(e.first!=node && e.second!=node)
{
excludedNode[node].addEdge(e.first, e.second);
}
}
}
}
void Link(int A, int B)
{
G.addEdge(A, B, true);
if(G.maxDeg==3)
{
if(excludedNode.empty()==true)
{
initFirst();
}
else
{
for(auto it = excludedNode.begin();it!=excludedNode.end();it++)
{
int x = it->first;
if(A!=x && B!=x)
{
it->second.addEdge(A, B);
}
}
}
}
else if(G.maxDeg>=4)
{
if(initSecondCalled==false)
{
for(auto it = excludedNode.begin();it!=excludedNode.end();it++)
{
int x = it->first;
if(A!=x && B!=x)
{
it->second.addEdge(A, B);
}
}
initSecond();
}
else
{
for(auto it = excludedNode.begin();it!=excludedNode.end();it++)
{
int x = it->first;
if(A!=x && B!=x)
{
it->second.addEdge(A, B);
}
}
}
}
}
int CountCritical()
{
if(G.maxDeg<=2)
{
if(G.T.badComponents>1) return 0;
if(G.T.badComponents==1) return G.T.badComponentSz;
return G.n;
}
int ans = 0;
for(auto it = excludedNode.begin();it!=excludedNode.end();it++)
{
if(it->second.T.badComponents==0 && it->second.maxDeg<=2)
{
ans++;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
72696 KB |
Output is correct |
2 |
Correct |
203 ms |
218360 KB |
Output is correct |
3 |
Correct |
208 ms |
218488 KB |
Output is correct |
4 |
Correct |
62 ms |
72824 KB |
Output is correct |
5 |
Correct |
62 ms |
72952 KB |
Output is correct |
6 |
Correct |
63 ms |
73080 KB |
Output is correct |
7 |
Correct |
202 ms |
217848 KB |
Output is correct |
8 |
Correct |
62 ms |
72952 KB |
Output is correct |
9 |
Correct |
215 ms |
218464 KB |
Output is correct |
10 |
Correct |
212 ms |
218488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
560 ms |
99784 KB |
Output is correct |
2 |
Runtime error |
639 ms |
262148 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
72696 KB |
Output is correct |
2 |
Correct |
203 ms |
218360 KB |
Output is correct |
3 |
Correct |
208 ms |
218488 KB |
Output is correct |
4 |
Correct |
62 ms |
72824 KB |
Output is correct |
5 |
Correct |
62 ms |
72952 KB |
Output is correct |
6 |
Correct |
63 ms |
73080 KB |
Output is correct |
7 |
Correct |
202 ms |
217848 KB |
Output is correct |
8 |
Correct |
62 ms |
72952 KB |
Output is correct |
9 |
Correct |
215 ms |
218464 KB |
Output is correct |
10 |
Correct |
212 ms |
218488 KB |
Output is correct |
11 |
Correct |
242 ms |
254916 KB |
Output is correct |
12 |
Correct |
250 ms |
255864 KB |
Output is correct |
13 |
Correct |
220 ms |
219384 KB |
Output is correct |
14 |
Correct |
214 ms |
218744 KB |
Output is correct |
15 |
Correct |
214 ms |
218616 KB |
Output is correct |
16 |
Correct |
65 ms |
73336 KB |
Output is correct |
17 |
Correct |
220 ms |
219424 KB |
Output is correct |
18 |
Correct |
257 ms |
256504 KB |
Output is correct |
19 |
Correct |
65 ms |
73336 KB |
Output is correct |
20 |
Correct |
219 ms |
219512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
72696 KB |
Output is correct |
2 |
Correct |
203 ms |
218360 KB |
Output is correct |
3 |
Correct |
208 ms |
218488 KB |
Output is correct |
4 |
Correct |
62 ms |
72824 KB |
Output is correct |
5 |
Correct |
62 ms |
72952 KB |
Output is correct |
6 |
Correct |
63 ms |
73080 KB |
Output is correct |
7 |
Correct |
202 ms |
217848 KB |
Output is correct |
8 |
Correct |
62 ms |
72952 KB |
Output is correct |
9 |
Correct |
215 ms |
218464 KB |
Output is correct |
10 |
Correct |
212 ms |
218488 KB |
Output is correct |
11 |
Correct |
242 ms |
254916 KB |
Output is correct |
12 |
Correct |
250 ms |
255864 KB |
Output is correct |
13 |
Correct |
220 ms |
219384 KB |
Output is correct |
14 |
Correct |
214 ms |
218744 KB |
Output is correct |
15 |
Correct |
214 ms |
218616 KB |
Output is correct |
16 |
Correct |
65 ms |
73336 KB |
Output is correct |
17 |
Correct |
220 ms |
219424 KB |
Output is correct |
18 |
Correct |
257 ms |
256504 KB |
Output is correct |
19 |
Correct |
65 ms |
73336 KB |
Output is correct |
20 |
Correct |
219 ms |
219512 KB |
Output is correct |
21 |
Correct |
80 ms |
74612 KB |
Output is correct |
22 |
Correct |
97 ms |
75760 KB |
Output is correct |
23 |
Correct |
107 ms |
76528 KB |
Output is correct |
24 |
Correct |
400 ms |
226416 KB |
Output is correct |
25 |
Correct |
233 ms |
220024 KB |
Output is correct |
26 |
Correct |
399 ms |
229488 KB |
Output is correct |
27 |
Correct |
116 ms |
77232 KB |
Output is correct |
28 |
Correct |
471 ms |
233196 KB |
Output is correct |
29 |
Runtime error |
316 ms |
262148 KB |
Execution killed with signal 9 |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
72696 KB |
Output is correct |
2 |
Correct |
203 ms |
218360 KB |
Output is correct |
3 |
Correct |
208 ms |
218488 KB |
Output is correct |
4 |
Correct |
62 ms |
72824 KB |
Output is correct |
5 |
Correct |
62 ms |
72952 KB |
Output is correct |
6 |
Correct |
63 ms |
73080 KB |
Output is correct |
7 |
Correct |
202 ms |
217848 KB |
Output is correct |
8 |
Correct |
62 ms |
72952 KB |
Output is correct |
9 |
Correct |
215 ms |
218464 KB |
Output is correct |
10 |
Correct |
212 ms |
218488 KB |
Output is correct |
11 |
Correct |
560 ms |
99784 KB |
Output is correct |
12 |
Runtime error |
639 ms |
262148 KB |
Execution killed with signal 9 |
13 |
Halted |
0 ms |
0 KB |
- |