#include <bits/stdc++.h>
using namespace std;
struct disjoint_sets
{
vector<size_t> repr, rank;
disjoint_sets(size_t n) : repr(n), rank(n)
{
for(size_t i = 0; i < n; i++)
repr[i] = i;
}
size_t find(size_t v)
{
return v == repr[v] ? v : repr[v] = find(repr[v]);
}
bool unite(size_t u, size_t v)
{
if((u = find(u)) == (v = find(v)))
return false;
if(rank[u] > rank[v])
swap(u, v);
rank[v] += (rank[u] == rank[v]);
repr[u] = v;
return true;
}
};
enum BambooForestStatus
{
Ok,
Cycles,
Bad
};
struct bamboo_forest
{
BambooForestStatus status = Ok;
disjoint_sets dsets;
vector<size_t> deg;
size_t cycles = 0;
bamboo_forest(size_t n) : dsets(n), deg(n) {}
bamboo_forest() : bamboo_forest(0) {}
BambooForestStatus edge(size_t u, size_t v)
{
deg[u]++; deg[v]++;
if(deg[u] > 2 or deg[v] > 2)
status = Bad;
if((status == Ok or status == Cycles) and dsets.find(u) == dsets.find(v) and deg[u] == 2 and deg[v] == 2)
status = Cycles, cycles++;
else if(not dsets.unite(u, v))
status = Bad;
return status;
}
};
size_t n;
vector<pair<size_t, size_t>> edges;
vector<vector<size_t>> graph;
bamboo_forest base;
size_t first_cycle_size;
unordered_set<size_t> greater2, greater3;
size_t w2 = SIZE_MAX, w3 = SIZE_MAX;
bamboo_forest drop_w3;
vector<pair<size_t, bamboo_forest>> drop_w2s;
void Init(int _n)
{
n = _n;
base = bamboo_forest(n);
graph.resize(n);
}
bamboo_forest current_forest_with_drop(size_t w)
{
bamboo_forest F(n);
for(auto [u, v] : edges)
if(u != w and v != w)
F.edge(u, v);
return F;
}
void Link(int _u, int _v)
{
size_t u = _u, v = _v;
edges.emplace_back(u, v);
graph[u].push_back(v);
graph[v].push_back(u);
auto pre = base.status;
base.edge(u, v);
if(pre == Ok and base.status == Cycles and base.cycles == 1)
{
auto w = base.dsets.find(u);
for(size_t i = 0; i < n; i++)
if(base.dsets.find(i) == w)
first_cycle_size++;
}
if(base.deg[u] > 2) greater2.insert(u);
if(base.deg[v] > 2) greater2.insert(v);
if(base.deg[u] > 3) greater3.insert(u);
if(base.deg[v] > 3) greater3.insert(v);
if(not greater3.empty() and w3 == SIZE_MAX)
drop_w3 = current_forest_with_drop(w3 = *greater3.begin());
else if(w3 != SIZE_MAX and u != w3 and v != w3)
drop_w3.edge(u, v);
if(not greater2.empty() and w2 == SIZE_MAX and w3 == SIZE_MAX)
{
w2 = *greater2.begin();
drop_w2s.resize(graph[w2].size() + 1, {SIZE_MAX, bamboo_forest(n)});
for(size_t i = 0; i < graph[w2].size(); i++)
drop_w2s[1+i] = {graph[w2][i], current_forest_with_drop(graph[w2][i])};
drop_w2s[0] = {w2, current_forest_with_drop(w2)};
}
else if(w3 == SIZE_MAX and w2 != SIZE_MAX)
{
for(auto& [w, F] : drop_w2s)
if(u != w and v != w)
F.edge(u, v);
}
}
bool naive_try(size_t i)
{
return current_forest_with_drop(i).status == Ok;
}
int CountCritical()
{
if(base.status == Ok)
return n;
else if(base.status == Cycles and base.cycles == 1)
return first_cycle_size;
else if(base.status == Cycles and base.cycles > 1)
return 0;
else if(greater3.size() > 1)
return 0;
else
{
if(w3 != SIZE_MAX)
return drop_w3.status == Ok;
size_t result = 0;
for(auto& [_, F] : drop_w2s)
result += F.status == Ok;
return result;
}
}
Compilation message
rings.cpp: In function 'int CountCritical()':
rings.cpp:154:24: warning: unused variable '_' [-Wunused-variable]
for(auto& [_, F] : drop_w2s)
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1536 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1408 KB |
Output is correct |
10 |
Correct |
4 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
669 ms |
49232 KB |
Output is correct |
2 |
Correct |
1581 ms |
170572 KB |
Output is correct |
3 |
Correct |
1951 ms |
230384 KB |
Output is correct |
4 |
Correct |
1337 ms |
94396 KB |
Output is correct |
5 |
Correct |
1436 ms |
94408 KB |
Output is correct |
6 |
Correct |
1415 ms |
92648 KB |
Output is correct |
7 |
Correct |
1911 ms |
228364 KB |
Output is correct |
8 |
Correct |
2219 ms |
195220 KB |
Output is correct |
9 |
Correct |
2412 ms |
210972 KB |
Output is correct |
10 |
Correct |
901 ms |
91656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1536 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1408 KB |
Output is correct |
10 |
Correct |
4 ms |
1408 KB |
Output is correct |
11 |
Correct |
4 ms |
1536 KB |
Output is correct |
12 |
Correct |
8 ms |
2576 KB |
Output is correct |
13 |
Correct |
10 ms |
2580 KB |
Output is correct |
14 |
Correct |
5 ms |
2324 KB |
Output is correct |
15 |
Correct |
7 ms |
4128 KB |
Output is correct |
16 |
Correct |
6 ms |
1408 KB |
Output is correct |
17 |
Correct |
8 ms |
2328 KB |
Output is correct |
18 |
Correct |
10 ms |
4384 KB |
Output is correct |
19 |
Correct |
5 ms |
1408 KB |
Output is correct |
20 |
Correct |
8 ms |
2580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1536 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1408 KB |
Output is correct |
10 |
Correct |
4 ms |
1408 KB |
Output is correct |
11 |
Correct |
4 ms |
1536 KB |
Output is correct |
12 |
Correct |
8 ms |
2576 KB |
Output is correct |
13 |
Correct |
10 ms |
2580 KB |
Output is correct |
14 |
Correct |
5 ms |
2324 KB |
Output is correct |
15 |
Correct |
7 ms |
4128 KB |
Output is correct |
16 |
Correct |
6 ms |
1408 KB |
Output is correct |
17 |
Correct |
8 ms |
2328 KB |
Output is correct |
18 |
Correct |
10 ms |
4384 KB |
Output is correct |
19 |
Correct |
5 ms |
1408 KB |
Output is correct |
20 |
Correct |
8 ms |
2580 KB |
Output is correct |
21 |
Correct |
23 ms |
4088 KB |
Output is correct |
22 |
Correct |
41 ms |
6384 KB |
Output is correct |
23 |
Correct |
64 ms |
8044 KB |
Output is correct |
24 |
Correct |
101 ms |
18680 KB |
Output is correct |
25 |
Correct |
53 ms |
17384 KB |
Output is correct |
26 |
Correct |
93 ms |
19144 KB |
Output is correct |
27 |
Correct |
55 ms |
8556 KB |
Output is correct |
28 |
Correct |
126 ms |
17756 KB |
Output is correct |
29 |
Correct |
62 ms |
19568 KB |
Output is correct |
30 |
Correct |
114 ms |
10732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1536 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
1152 KB |
Output is correct |
8 |
Correct |
2 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1408 KB |
Output is correct |
10 |
Correct |
4 ms |
1408 KB |
Output is correct |
11 |
Correct |
669 ms |
49232 KB |
Output is correct |
12 |
Correct |
1581 ms |
170572 KB |
Output is correct |
13 |
Correct |
1951 ms |
230384 KB |
Output is correct |
14 |
Correct |
1337 ms |
94396 KB |
Output is correct |
15 |
Correct |
1436 ms |
94408 KB |
Output is correct |
16 |
Correct |
1415 ms |
92648 KB |
Output is correct |
17 |
Correct |
1911 ms |
228364 KB |
Output is correct |
18 |
Correct |
2219 ms |
195220 KB |
Output is correct |
19 |
Correct |
2412 ms |
210972 KB |
Output is correct |
20 |
Correct |
901 ms |
91656 KB |
Output is correct |
21 |
Correct |
4 ms |
1536 KB |
Output is correct |
22 |
Correct |
8 ms |
2576 KB |
Output is correct |
23 |
Correct |
10 ms |
2580 KB |
Output is correct |
24 |
Correct |
5 ms |
2324 KB |
Output is correct |
25 |
Correct |
7 ms |
4128 KB |
Output is correct |
26 |
Correct |
6 ms |
1408 KB |
Output is correct |
27 |
Correct |
8 ms |
2328 KB |
Output is correct |
28 |
Correct |
10 ms |
4384 KB |
Output is correct |
29 |
Correct |
5 ms |
1408 KB |
Output is correct |
30 |
Correct |
8 ms |
2580 KB |
Output is correct |
31 |
Correct |
23 ms |
4088 KB |
Output is correct |
32 |
Correct |
41 ms |
6384 KB |
Output is correct |
33 |
Correct |
64 ms |
8044 KB |
Output is correct |
34 |
Correct |
101 ms |
18680 KB |
Output is correct |
35 |
Correct |
53 ms |
17384 KB |
Output is correct |
36 |
Correct |
93 ms |
19144 KB |
Output is correct |
37 |
Correct |
55 ms |
8556 KB |
Output is correct |
38 |
Correct |
126 ms |
17756 KB |
Output is correct |
39 |
Correct |
62 ms |
19568 KB |
Output is correct |
40 |
Correct |
114 ms |
10732 KB |
Output is correct |
41 |
Correct |
305 ms |
39136 KB |
Output is correct |
42 |
Correct |
938 ms |
160976 KB |
Output is correct |
43 |
Correct |
406 ms |
160664 KB |
Output is correct |
44 |
Correct |
1128 ms |
206600 KB |
Output is correct |
45 |
Correct |
1361 ms |
215872 KB |
Output is correct |
46 |
Correct |
879 ms |
89608 KB |
Output is correct |
47 |
Correct |
1149 ms |
91200 KB |
Output is correct |
48 |
Correct |
804 ms |
195884 KB |
Output is correct |
49 |
Correct |
847 ms |
94412 KB |
Output is correct |
50 |
Correct |
968 ms |
93544 KB |
Output is correct |
51 |
Correct |
451 ms |
141268 KB |
Output is correct |
52 |
Correct |
1310 ms |
148772 KB |
Output is correct |
53 |
Correct |
820 ms |
195792 KB |
Output is correct |
54 |
Correct |
2265 ms |
163340 KB |
Output is correct |
55 |
Correct |
1622 ms |
198580 KB |
Output is correct |