#include<bits/stdc++.h>
using namespace std;
int N;
int current_state, ans; // 0: all chain, 1: ring, 2: 1 non-ring 3: return 0
const int maxn = 1e6+5;
int up[maxn], L[maxn], R[maxn];
vector<pair<int, int>> E;
vector<int> g[maxn];
int Find(int x){
return up[x] < 0 ? x : up[x] = Find(up[x]);
}
struct dsu{
int nd, state;
vector<int> up, L, R;
int Find(int x){
return up[x] < 0 ? x : up[x] = Find(up[x]);
}
void Union(int a, int b){
// cout << "UNION " << a << " " << b << endl;
if(a == nd || b == nd)return;
if(!state)return;
int ra = Find(a), rb = Find(b);
if(ra == rb){
state = 0;
return;
}
if(a != L[ra])swap(L[ra], R[ra]);
if(b != L[rb])swap(L[rb], R[rb]);
if(a != L[ra] || b != L[rb]){
state = 0;
return;
}
up[rb] = ra;
L[ra] = R[rb];
}
dsu(int _nd): nd(_nd){
up.assign(N, -1);
L.resize(N);
R.resize(N);
for(int i = 0; i < N; i++)L[i] = R[i] = i;
state = 1;
for(auto v: E)Union(v.first, v.second);
}
};
vector<dsu> candidates;
void init_cen(int x){
current_state = 2;
candidates.push_back(dsu(x));
for(auto v: g[x])candidates.push_back(dsu(v));
}
void Union(int a, int b){ // only happens while in state 0 or 1
int ra = Find(a), rb = Find(b);
if(ra == rb){
if(current_state == 1){
current_state = 3;
return;
}
L[ra] = R[ra] = -1;
current_state = 1;
ans = -up[ra];
}
else {
if(a != L[ra])swap(L[ra], R[ra]);
if(b != L[rb])swap(L[rb], R[rb]);
up[ra] += up[rb];
up[rb] = ra;
L[ra] = R[rb];
}
}
void Init(int N_) {
current_state = 0;
N = N_;
ans = N;
candidates.clear();
E.clear();
for(int i = 0; i < N; i++){
g[i].clear();
up[i] = -1;
L[i] = R[i] = i;
}
}
void Link(int A, int B) {
E.push_back({A, B});
g[A].push_back(B);
g[B].push_back(A);
if(current_state <= 1){
if(g[A].size() >= 3)init_cen(A);
else if(g[B].size() >= 3)init_cen(B);
else Union(A, B);
}
else {
for(auto &v: candidates)v.Union(A, B);
}
}
int CountCritical() {
if(current_state <= 1)return ans;
else {
int sum = 0;
for(auto &v: candidates)sum += v.state;
return sum;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
24416 KB |
Output is correct |
3 |
Correct |
16 ms |
24668 KB |
Output is correct |
4 |
Correct |
13 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
24088 KB |
Output is correct |
6 |
Correct |
10 ms |
24324 KB |
Output is correct |
7 |
Correct |
9 ms |
24156 KB |
Output is correct |
8 |
Correct |
10 ms |
24228 KB |
Output is correct |
9 |
Correct |
11 ms |
24392 KB |
Output is correct |
10 |
Correct |
11 ms |
24412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
220 ms |
57152 KB |
Output is correct |
2 |
Correct |
589 ms |
112808 KB |
Output is correct |
3 |
Correct |
489 ms |
129964 KB |
Output is correct |
4 |
Correct |
662 ms |
87976 KB |
Output is correct |
5 |
Correct |
604 ms |
87984 KB |
Output is correct |
6 |
Correct |
557 ms |
86272 KB |
Output is correct |
7 |
Correct |
421 ms |
128432 KB |
Output is correct |
8 |
Correct |
658 ms |
127908 KB |
Output is correct |
9 |
Correct |
704 ms |
135080 KB |
Output is correct |
10 |
Correct |
426 ms |
85352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
24416 KB |
Output is correct |
3 |
Correct |
16 ms |
24668 KB |
Output is correct |
4 |
Correct |
13 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
24088 KB |
Output is correct |
6 |
Correct |
10 ms |
24324 KB |
Output is correct |
7 |
Correct |
9 ms |
24156 KB |
Output is correct |
8 |
Correct |
10 ms |
24228 KB |
Output is correct |
9 |
Correct |
11 ms |
24392 KB |
Output is correct |
10 |
Correct |
11 ms |
24412 KB |
Output is correct |
11 |
Correct |
12 ms |
24408 KB |
Output is correct |
12 |
Correct |
11 ms |
24924 KB |
Output is correct |
13 |
Correct |
11 ms |
24924 KB |
Output is correct |
14 |
Correct |
11 ms |
24668 KB |
Output is correct |
15 |
Correct |
11 ms |
25244 KB |
Output is correct |
16 |
Correct |
12 ms |
24412 KB |
Output is correct |
17 |
Correct |
11 ms |
24924 KB |
Output is correct |
18 |
Correct |
19 ms |
25692 KB |
Output is correct |
19 |
Correct |
20 ms |
24556 KB |
Output is correct |
20 |
Correct |
12 ms |
24944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
24416 KB |
Output is correct |
3 |
Correct |
16 ms |
24668 KB |
Output is correct |
4 |
Correct |
13 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
24088 KB |
Output is correct |
6 |
Correct |
10 ms |
24324 KB |
Output is correct |
7 |
Correct |
9 ms |
24156 KB |
Output is correct |
8 |
Correct |
10 ms |
24228 KB |
Output is correct |
9 |
Correct |
11 ms |
24392 KB |
Output is correct |
10 |
Correct |
11 ms |
24412 KB |
Output is correct |
11 |
Correct |
12 ms |
24408 KB |
Output is correct |
12 |
Correct |
11 ms |
24924 KB |
Output is correct |
13 |
Correct |
11 ms |
24924 KB |
Output is correct |
14 |
Correct |
11 ms |
24668 KB |
Output is correct |
15 |
Correct |
11 ms |
25244 KB |
Output is correct |
16 |
Correct |
12 ms |
24412 KB |
Output is correct |
17 |
Correct |
11 ms |
24924 KB |
Output is correct |
18 |
Correct |
19 ms |
25692 KB |
Output is correct |
19 |
Correct |
20 ms |
24556 KB |
Output is correct |
20 |
Correct |
12 ms |
24944 KB |
Output is correct |
21 |
Correct |
19 ms |
25940 KB |
Output is correct |
22 |
Correct |
28 ms |
27340 KB |
Output is correct |
23 |
Correct |
32 ms |
28372 KB |
Output is correct |
24 |
Correct |
34 ms |
31956 KB |
Output is correct |
25 |
Correct |
17 ms |
30156 KB |
Output is correct |
26 |
Correct |
41 ms |
32972 KB |
Output is correct |
27 |
Correct |
39 ms |
29128 KB |
Output is correct |
28 |
Correct |
35 ms |
33480 KB |
Output is correct |
29 |
Correct |
38 ms |
32216 KB |
Output is correct |
30 |
Correct |
42 ms |
29896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
12 ms |
24416 KB |
Output is correct |
3 |
Correct |
16 ms |
24668 KB |
Output is correct |
4 |
Correct |
13 ms |
23900 KB |
Output is correct |
5 |
Correct |
10 ms |
24088 KB |
Output is correct |
6 |
Correct |
10 ms |
24324 KB |
Output is correct |
7 |
Correct |
9 ms |
24156 KB |
Output is correct |
8 |
Correct |
10 ms |
24228 KB |
Output is correct |
9 |
Correct |
11 ms |
24392 KB |
Output is correct |
10 |
Correct |
11 ms |
24412 KB |
Output is correct |
11 |
Correct |
220 ms |
57152 KB |
Output is correct |
12 |
Correct |
589 ms |
112808 KB |
Output is correct |
13 |
Correct |
489 ms |
129964 KB |
Output is correct |
14 |
Correct |
662 ms |
87976 KB |
Output is correct |
15 |
Correct |
604 ms |
87984 KB |
Output is correct |
16 |
Correct |
557 ms |
86272 KB |
Output is correct |
17 |
Correct |
421 ms |
128432 KB |
Output is correct |
18 |
Correct |
658 ms |
127908 KB |
Output is correct |
19 |
Correct |
704 ms |
135080 KB |
Output is correct |
20 |
Correct |
426 ms |
85352 KB |
Output is correct |
21 |
Correct |
12 ms |
24408 KB |
Output is correct |
22 |
Correct |
11 ms |
24924 KB |
Output is correct |
23 |
Correct |
11 ms |
24924 KB |
Output is correct |
24 |
Correct |
11 ms |
24668 KB |
Output is correct |
25 |
Correct |
11 ms |
25244 KB |
Output is correct |
26 |
Correct |
12 ms |
24412 KB |
Output is correct |
27 |
Correct |
11 ms |
24924 KB |
Output is correct |
28 |
Correct |
19 ms |
25692 KB |
Output is correct |
29 |
Correct |
20 ms |
24556 KB |
Output is correct |
30 |
Correct |
12 ms |
24944 KB |
Output is correct |
31 |
Correct |
19 ms |
25940 KB |
Output is correct |
32 |
Correct |
28 ms |
27340 KB |
Output is correct |
33 |
Correct |
32 ms |
28372 KB |
Output is correct |
34 |
Correct |
34 ms |
31956 KB |
Output is correct |
35 |
Correct |
17 ms |
30156 KB |
Output is correct |
36 |
Correct |
41 ms |
32972 KB |
Output is correct |
37 |
Correct |
39 ms |
29128 KB |
Output is correct |
38 |
Correct |
35 ms |
33480 KB |
Output is correct |
39 |
Correct |
38 ms |
32216 KB |
Output is correct |
40 |
Correct |
42 ms |
29896 KB |
Output is correct |
41 |
Correct |
131 ms |
43320 KB |
Output is correct |
42 |
Correct |
325 ms |
99244 KB |
Output is correct |
43 |
Correct |
161 ms |
85616 KB |
Output is correct |
44 |
Correct |
334 ms |
125264 KB |
Output is correct |
45 |
Correct |
347 ms |
117824 KB |
Output is correct |
46 |
Correct |
358 ms |
77996 KB |
Output is correct |
47 |
Correct |
480 ms |
78836 KB |
Output is correct |
48 |
Correct |
242 ms |
107964 KB |
Output is correct |
49 |
Correct |
346 ms |
77028 KB |
Output is correct |
50 |
Correct |
396 ms |
76436 KB |
Output is correct |
51 |
Correct |
159 ms |
80264 KB |
Output is correct |
52 |
Correct |
324 ms |
105756 KB |
Output is correct |
53 |
Correct |
226 ms |
108620 KB |
Output is correct |
54 |
Correct |
577 ms |
114360 KB |
Output is correct |
55 |
Correct |
536 ms |
122292 KB |
Output is correct |