#include <bits/stdc++.h>
using namespace std;
const int k_N = 1e6 + 3;
struct DSU{
int p[k_N], sz[k_N];
int d[k_N];
int cycles_all, mx_all;
int cycle_node;
DSU(){
}
void update_d(int u){
d[u]++;
}
void resize(int n){
cycles_all = 0;
mx_all = 0;
cycle_node = -1;
for(int i = 0; i < n; ++i){
p[i] = i;
sz[i] = 1;
d[i] = 0;
}
}
int get_p(int u){
if(p[u] == u) return u;
return p[u] = get_p(p[u]);
}
bool unite(int u, int v){
if(get_p(u) == get_p(v))
return false;
if(sz[p[u]] < sz[p[v]])
swap(u, v);
sz[p[u]] += sz[p[v]];
p[p[v]] = p[u];
return true;
}
void add(int a, int b){
update_d(a);
update_d(b);
mx_all = max(mx_all, max(d[a], d[b]));
if(!unite(a, b)){
cycle_node = a;
cycles_all++;
}
}
};
int n;
vector<pair<int, int>> edges;
vector<int> adj[k_N];
DSU dsu, dsu_cand[4];
int cand[4];
void Init(int n_){
n = n_;
dsu.resize(n);
}
void do_cand(int a){
cand[0] = a;
for(int i = 0; i < adj[a].size(); ++i)
cand[i + 1] = adj[a][i];
for(int i = 0; i < 4; ++i){
dsu_cand[i].resize(n);
for(auto [u, v]: edges){
if(u == cand[i] || v == cand[i]) continue;
dsu_cand[i].add(u, v);
}
}
}
void Link(int a, int b){
edges.push_back({a, b});
adj[a].push_back(b);
adj[b].push_back(a);
if(dsu.mx_all < 3 && adj[a].size() == 3)
do_cand(a);
else if(dsu.mx_all < 3 && adj[b].size() == 3)
do_cand(b);
if(dsu.mx_all >= 3){
for(int i = 0; i < 4; ++i){
if(a == cand[i] || b == cand[i]) continue;
dsu_cand[i].add(a, b);
}
}
dsu.add(a, b);
}
int CountCritical(){
if(dsu.mx_all <= 2){
if(dsu.cycles_all >= 2)
return 0;
if(dsu.cycles_all == 1)
return dsu.sz[dsu.get_p(dsu.cycle_node)];
return n;
}
int ans = 0;
for(int i = 0; i < 4; ++i){
if(dsu_cand[i].mx_all < 3 && dsu_cand[i].cycles_all == 0)
ans++;
}
return ans;
}
Compilation message
rings.cpp: In function 'void do_cand(int)':
rings.cpp:80:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[a].size(); ++i)
~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24576 KB |
Output is correct |
4 |
Correct |
15 ms |
23936 KB |
Output is correct |
5 |
Correct |
18 ms |
24064 KB |
Output is correct |
6 |
Correct |
19 ms |
24192 KB |
Output is correct |
7 |
Correct |
15 ms |
24320 KB |
Output is correct |
8 |
Correct |
16 ms |
24064 KB |
Output is correct |
9 |
Correct |
18 ms |
24576 KB |
Output is correct |
10 |
Correct |
18 ms |
24512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
534 ms |
50396 KB |
Output is correct |
2 |
Correct |
1977 ms |
102224 KB |
Output is correct |
3 |
Correct |
2073 ms |
117168 KB |
Output is correct |
4 |
Correct |
1429 ms |
74704 KB |
Output is correct |
5 |
Correct |
1245 ms |
74712 KB |
Output is correct |
6 |
Correct |
1194 ms |
73424 KB |
Output is correct |
7 |
Correct |
1881 ms |
116380 KB |
Output is correct |
8 |
Correct |
1944 ms |
115548 KB |
Output is correct |
9 |
Correct |
2005 ms |
135188 KB |
Output is correct |
10 |
Correct |
904 ms |
85404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24576 KB |
Output is correct |
4 |
Correct |
15 ms |
23936 KB |
Output is correct |
5 |
Correct |
18 ms |
24064 KB |
Output is correct |
6 |
Correct |
19 ms |
24192 KB |
Output is correct |
7 |
Correct |
15 ms |
24320 KB |
Output is correct |
8 |
Correct |
16 ms |
24064 KB |
Output is correct |
9 |
Correct |
18 ms |
24576 KB |
Output is correct |
10 |
Correct |
18 ms |
24512 KB |
Output is correct |
11 |
Correct |
18 ms |
24448 KB |
Output is correct |
12 |
Correct |
23 ms |
25080 KB |
Output is correct |
13 |
Correct |
21 ms |
25080 KB |
Output is correct |
14 |
Correct |
23 ms |
24832 KB |
Output is correct |
15 |
Correct |
21 ms |
25472 KB |
Output is correct |
16 |
Correct |
22 ms |
24448 KB |
Output is correct |
17 |
Correct |
21 ms |
25088 KB |
Output is correct |
18 |
Correct |
22 ms |
25728 KB |
Output is correct |
19 |
Correct |
20 ms |
24576 KB |
Output is correct |
20 |
Correct |
21 ms |
25088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24576 KB |
Output is correct |
4 |
Correct |
15 ms |
23936 KB |
Output is correct |
5 |
Correct |
18 ms |
24064 KB |
Output is correct |
6 |
Correct |
19 ms |
24192 KB |
Output is correct |
7 |
Correct |
15 ms |
24320 KB |
Output is correct |
8 |
Correct |
16 ms |
24064 KB |
Output is correct |
9 |
Correct |
18 ms |
24576 KB |
Output is correct |
10 |
Correct |
18 ms |
24512 KB |
Output is correct |
11 |
Correct |
18 ms |
24448 KB |
Output is correct |
12 |
Correct |
23 ms |
25080 KB |
Output is correct |
13 |
Correct |
21 ms |
25080 KB |
Output is correct |
14 |
Correct |
23 ms |
24832 KB |
Output is correct |
15 |
Correct |
21 ms |
25472 KB |
Output is correct |
16 |
Correct |
22 ms |
24448 KB |
Output is correct |
17 |
Correct |
21 ms |
25088 KB |
Output is correct |
18 |
Correct |
22 ms |
25728 KB |
Output is correct |
19 |
Correct |
20 ms |
24576 KB |
Output is correct |
20 |
Correct |
21 ms |
25088 KB |
Output is correct |
21 |
Correct |
39 ms |
26108 KB |
Output is correct |
22 |
Correct |
49 ms |
27248 KB |
Output is correct |
23 |
Correct |
59 ms |
28272 KB |
Output is correct |
24 |
Correct |
90 ms |
32112 KB |
Output is correct |
25 |
Correct |
37 ms |
30328 KB |
Output is correct |
26 |
Correct |
95 ms |
33008 KB |
Output is correct |
27 |
Correct |
69 ms |
29292 KB |
Output is correct |
28 |
Correct |
100 ms |
33644 KB |
Output is correct |
29 |
Correct |
68 ms |
32244 KB |
Output is correct |
30 |
Correct |
89 ms |
30060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24576 KB |
Output is correct |
4 |
Correct |
15 ms |
23936 KB |
Output is correct |
5 |
Correct |
18 ms |
24064 KB |
Output is correct |
6 |
Correct |
19 ms |
24192 KB |
Output is correct |
7 |
Correct |
15 ms |
24320 KB |
Output is correct |
8 |
Correct |
16 ms |
24064 KB |
Output is correct |
9 |
Correct |
18 ms |
24576 KB |
Output is correct |
10 |
Correct |
18 ms |
24512 KB |
Output is correct |
11 |
Correct |
534 ms |
50396 KB |
Output is correct |
12 |
Correct |
1977 ms |
102224 KB |
Output is correct |
13 |
Correct |
2073 ms |
117168 KB |
Output is correct |
14 |
Correct |
1429 ms |
74704 KB |
Output is correct |
15 |
Correct |
1245 ms |
74712 KB |
Output is correct |
16 |
Correct |
1194 ms |
73424 KB |
Output is correct |
17 |
Correct |
1881 ms |
116380 KB |
Output is correct |
18 |
Correct |
1944 ms |
115548 KB |
Output is correct |
19 |
Correct |
2005 ms |
135188 KB |
Output is correct |
20 |
Correct |
904 ms |
85404 KB |
Output is correct |
21 |
Correct |
18 ms |
24448 KB |
Output is correct |
22 |
Correct |
23 ms |
25080 KB |
Output is correct |
23 |
Correct |
21 ms |
25080 KB |
Output is correct |
24 |
Correct |
23 ms |
24832 KB |
Output is correct |
25 |
Correct |
21 ms |
25472 KB |
Output is correct |
26 |
Correct |
22 ms |
24448 KB |
Output is correct |
27 |
Correct |
21 ms |
25088 KB |
Output is correct |
28 |
Correct |
22 ms |
25728 KB |
Output is correct |
29 |
Correct |
20 ms |
24576 KB |
Output is correct |
30 |
Correct |
21 ms |
25088 KB |
Output is correct |
31 |
Correct |
39 ms |
26108 KB |
Output is correct |
32 |
Correct |
49 ms |
27248 KB |
Output is correct |
33 |
Correct |
59 ms |
28272 KB |
Output is correct |
34 |
Correct |
90 ms |
32112 KB |
Output is correct |
35 |
Correct |
37 ms |
30328 KB |
Output is correct |
36 |
Correct |
95 ms |
33008 KB |
Output is correct |
37 |
Correct |
69 ms |
29292 KB |
Output is correct |
38 |
Correct |
100 ms |
33644 KB |
Output is correct |
39 |
Correct |
68 ms |
32244 KB |
Output is correct |
40 |
Correct |
89 ms |
30060 KB |
Output is correct |
41 |
Correct |
293 ms |
43492 KB |
Output is correct |
42 |
Correct |
888 ms |
99428 KB |
Output is correct |
43 |
Correct |
352 ms |
85744 KB |
Output is correct |
44 |
Correct |
1391 ms |
125264 KB |
Output is correct |
45 |
Correct |
1390 ms |
117708 KB |
Output is correct |
46 |
Correct |
896 ms |
78160 KB |
Output is correct |
47 |
Correct |
1252 ms |
79064 KB |
Output is correct |
48 |
Correct |
947 ms |
108124 KB |
Output is correct |
49 |
Correct |
808 ms |
77148 KB |
Output is correct |
50 |
Correct |
897 ms |
76756 KB |
Output is correct |
51 |
Correct |
429 ms |
80236 KB |
Output is correct |
52 |
Correct |
1297 ms |
106096 KB |
Output is correct |
53 |
Correct |
898 ms |
108768 KB |
Output is correct |
54 |
Correct |
1647 ms |
114644 KB |
Output is correct |
55 |
Correct |
1824 ms |
122460 KB |
Output is correct |