#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 w3 = SIZE_MAX;
bamboo_forest drop_w3;
void Init(int _n)
{
n = _n;
base = bamboo_forest(n);
graph.resize(n);
}
void Link(int u, int 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)
{
w3 = *greater3.begin();
drop_w3 = bamboo_forest(n);
for(auto [uu, vv] : edges)
if(uu != w3 and vv != w3)
drop_w3.edge(uu, vv);
}
else if(w3 != SIZE_MAX and (size_t)u != w3 and (size_t)v != w3)
drop_w3.edge(u, v);
}
bool naive_try(size_t i)
{
bamboo_forest re(n);
for(auto [u, v] : edges)
if(u != i and v != i)
re.edge(u, v);
return re.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, w = *greater2.begin();
for(size_t i : graph[w])
result += naive_try(i);
result += naive_try(w);
return result;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 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 |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
545 ms |
49340 KB |
Output is correct |
2 |
Correct |
1168 ms |
95292 KB |
Output is correct |
3 |
Correct |
1858 ms |
136264 KB |
Output is correct |
4 |
Correct |
1270 ms |
94284 KB |
Output is correct |
5 |
Correct |
1282 ms |
94404 KB |
Output is correct |
6 |
Correct |
1443 ms |
92608 KB |
Output is correct |
7 |
Correct |
1769 ms |
134428 KB |
Output is correct |
8 |
Correct |
1921 ms |
110344 KB |
Output is correct |
9 |
Correct |
2304 ms |
118104 KB |
Output is correct |
10 |
Correct |
885 ms |
91860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 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 |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
14 ms |
1024 KB |
Output is correct |
12 |
Correct |
12 ms |
1680 KB |
Output is correct |
13 |
Correct |
37 ms |
1820 KB |
Output is correct |
14 |
Correct |
6 ms |
1408 KB |
Output is correct |
15 |
Correct |
5 ms |
2176 KB |
Output is correct |
16 |
Correct |
7 ms |
1408 KB |
Output is correct |
17 |
Correct |
67 ms |
2204 KB |
Output is correct |
18 |
Correct |
15 ms |
2584 KB |
Output is correct |
19 |
Correct |
6 ms |
1408 KB |
Output is correct |
20 |
Correct |
58 ms |
1604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 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 |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
14 ms |
1024 KB |
Output is correct |
12 |
Correct |
12 ms |
1680 KB |
Output is correct |
13 |
Correct |
37 ms |
1820 KB |
Output is correct |
14 |
Correct |
6 ms |
1408 KB |
Output is correct |
15 |
Correct |
5 ms |
2176 KB |
Output is correct |
16 |
Correct |
7 ms |
1408 KB |
Output is correct |
17 |
Correct |
67 ms |
2204 KB |
Output is correct |
18 |
Correct |
15 ms |
2584 KB |
Output is correct |
19 |
Correct |
6 ms |
1408 KB |
Output is correct |
20 |
Correct |
58 ms |
1604 KB |
Output is correct |
21 |
Correct |
23 ms |
4216 KB |
Output is correct |
22 |
Correct |
38 ms |
6384 KB |
Output is correct |
23 |
Correct |
51 ms |
7916 KB |
Output is correct |
24 |
Correct |
70 ms |
10600 KB |
Output is correct |
25 |
Correct |
34 ms |
8236 KB |
Output is correct |
26 |
Correct |
610 ms |
11224 KB |
Output is correct |
27 |
Correct |
62 ms |
9580 KB |
Output is correct |
28 |
Execution timed out |
4033 ms |
8952 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
768 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 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 |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
768 KB |
Output is correct |
9 |
Correct |
4 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
1024 KB |
Output is correct |
11 |
Correct |
545 ms |
49340 KB |
Output is correct |
12 |
Correct |
1168 ms |
95292 KB |
Output is correct |
13 |
Correct |
1858 ms |
136264 KB |
Output is correct |
14 |
Correct |
1270 ms |
94284 KB |
Output is correct |
15 |
Correct |
1282 ms |
94404 KB |
Output is correct |
16 |
Correct |
1443 ms |
92608 KB |
Output is correct |
17 |
Correct |
1769 ms |
134428 KB |
Output is correct |
18 |
Correct |
1921 ms |
110344 KB |
Output is correct |
19 |
Correct |
2304 ms |
118104 KB |
Output is correct |
20 |
Correct |
885 ms |
91860 KB |
Output is correct |
21 |
Correct |
14 ms |
1024 KB |
Output is correct |
22 |
Correct |
12 ms |
1680 KB |
Output is correct |
23 |
Correct |
37 ms |
1820 KB |
Output is correct |
24 |
Correct |
6 ms |
1408 KB |
Output is correct |
25 |
Correct |
5 ms |
2176 KB |
Output is correct |
26 |
Correct |
7 ms |
1408 KB |
Output is correct |
27 |
Correct |
67 ms |
2204 KB |
Output is correct |
28 |
Correct |
15 ms |
2584 KB |
Output is correct |
29 |
Correct |
6 ms |
1408 KB |
Output is correct |
30 |
Correct |
58 ms |
1604 KB |
Output is correct |
31 |
Correct |
23 ms |
4216 KB |
Output is correct |
32 |
Correct |
38 ms |
6384 KB |
Output is correct |
33 |
Correct |
51 ms |
7916 KB |
Output is correct |
34 |
Correct |
70 ms |
10600 KB |
Output is correct |
35 |
Correct |
34 ms |
8236 KB |
Output is correct |
36 |
Correct |
610 ms |
11224 KB |
Output is correct |
37 |
Correct |
62 ms |
9580 KB |
Output is correct |
38 |
Execution timed out |
4033 ms |
8952 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |