#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
//#define int ll
#define sz(x) (int)(x).size()
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 1e6 + 5;
int n;
struct DSU {
int forb;
int dsu[nmax], area[nmax];
int grad[nmax];
bool fail = 0;
int f(int x) { return (dsu[x] == x? x : dsu[x] = f(dsu[x])); }
void unite(int x, int y) {
if(x == forb || y == forb) return;
// cerr << x << ' ' << y << ' ' << forb << '\t';
grad[x]++;
grad[y]++;
if(grad[x] > 2 || grad[y] > 2)
fail = 1;
x = f(x);
y = f(y);
// cerr << x << ' ' << y << '\n';
if(x == y) { fail = 1; return; }
dsu[x] = y;
area[y] += area[x];
}
void init(int n, vector<pii> edg, int interz = -1) {
forb = interz;
for(int i = 0; i < n; i++)
dsu[i] = i, area[i] = 1, grad[i] = 0;
for(auto [a, b] : edg)
unite(a, b);
}
bool query() { return !fail; }
};
namespace Finite {
int freq[nmax];
vector<DSU> dlist;
int mgrad = 0;
int precrez = n;
vector<pii> edg;
vector<int> g[nmax];
void init3(int a) {
dlist.clear();
for(auto x : g[a])
dlist.resize(sz(dlist) + 1),
dlist.back().init(n, edg, x);
dlist.resize(sz(dlist) + 1),
dlist.back().init(n, edg, a);
return;
}
void init4(int a) {
dlist.clear();
dlist.resize(sz(dlist) + 1),
dlist.back().init(n, edg, a);
return;
}
void upd(int a, int b) {
g[a].emplace_back(b);
g[b].emplace_back(a);
edg.emplace_back(a, b);
int fmgrad = mgrad;
freq[a]++;
freq[b]++;
mgrad = max({mgrad, freq[a], freq[b]});
if(fmgrad != mgrad) {
if(mgrad == 3)
init3(freq[a] == 3? a : b);
if(mgrad == 4)
init4(freq[a] == 4? a : b);
if(mgrad > 2) return;
}
if(mgrad == 2) {
// cerr << a << ' ' << b << '\n';
// cerr << dlist[0].f(a) << ' ' << dlist[0].f(b) << '\n';
if(dlist[0].f(a) == dlist[0].f(b)) {
if(precrez == n) {
// cerr << "lamborghini\n";
precrez = dlist[0].area[dlist[0].f(a)];
}
else
precrez = 0;
}
}
for(auto &x : dlist)
x.unite(a, b);
return;
}
int get() {
if(mgrad >= 3) {
int sum = 0;
for(auto &x : dlist)
sum += x.query();
return sum;
}
else
return precrez;
}
void init() {
precrez = n;
dlist.resize(1);
dlist.back().init(n, edg);
}
}
void Init(int N) {
n = N;
Finite::init();
}
void Link(int A, int B) {
Finite::upd(A, B);
//cerr << " > " << Finite::get() << '\n';
}
int CountCritical() {
//cerr << Finite::state << '\t';
return Finite::get();
}
Compilation message
rings.cpp: In member function 'void DSU::init(int, std::vector<std::pair<int, int> >, int)':
rings.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for(auto [a, b] : edg)
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
46 ms |
82716 KB |
Output is correct |
3 |
Correct |
49 ms |
82672 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
21 ms |
35628 KB |
Output is correct |
6 |
Correct |
24 ms |
35808 KB |
Output is correct |
7 |
Correct |
43 ms |
82560 KB |
Output is correct |
8 |
Correct |
19 ms |
35760 KB |
Output is correct |
9 |
Correct |
52 ms |
82704 KB |
Output is correct |
10 |
Correct |
44 ms |
82724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
392 ms |
64760 KB |
Output is correct |
2 |
Correct |
1067 ms |
115812 KB |
Output is correct |
3 |
Correct |
1250 ms |
121916 KB |
Output is correct |
4 |
Correct |
1103 ms |
91900 KB |
Output is correct |
5 |
Correct |
1128 ms |
91900 KB |
Output is correct |
6 |
Correct |
1061 ms |
90340 KB |
Output is correct |
7 |
Correct |
1104 ms |
120568 KB |
Output is correct |
8 |
Correct |
1536 ms |
131376 KB |
Output is correct |
9 |
Correct |
1653 ms |
141092 KB |
Output is correct |
10 |
Correct |
753 ms |
89660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
46 ms |
82716 KB |
Output is correct |
3 |
Correct |
49 ms |
82672 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
21 ms |
35628 KB |
Output is correct |
6 |
Correct |
24 ms |
35808 KB |
Output is correct |
7 |
Correct |
43 ms |
82560 KB |
Output is correct |
8 |
Correct |
19 ms |
35760 KB |
Output is correct |
9 |
Correct |
52 ms |
82704 KB |
Output is correct |
10 |
Correct |
44 ms |
82724 KB |
Output is correct |
11 |
Correct |
49 ms |
82736 KB |
Output is correct |
12 |
Correct |
67 ms |
83136 KB |
Output is correct |
13 |
Correct |
51 ms |
82912 KB |
Output is correct |
14 |
Correct |
49 ms |
82684 KB |
Output is correct |
15 |
Correct |
46 ms |
82564 KB |
Output is correct |
16 |
Correct |
20 ms |
36052 KB |
Output is correct |
17 |
Correct |
65 ms |
82736 KB |
Output is correct |
18 |
Correct |
58 ms |
82824 KB |
Output is correct |
19 |
Correct |
21 ms |
36180 KB |
Output is correct |
20 |
Correct |
49 ms |
82992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
46 ms |
82716 KB |
Output is correct |
3 |
Correct |
49 ms |
82672 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
21 ms |
35628 KB |
Output is correct |
6 |
Correct |
24 ms |
35808 KB |
Output is correct |
7 |
Correct |
43 ms |
82560 KB |
Output is correct |
8 |
Correct |
19 ms |
35760 KB |
Output is correct |
9 |
Correct |
52 ms |
82704 KB |
Output is correct |
10 |
Correct |
44 ms |
82724 KB |
Output is correct |
11 |
Correct |
49 ms |
82736 KB |
Output is correct |
12 |
Correct |
67 ms |
83136 KB |
Output is correct |
13 |
Correct |
51 ms |
82912 KB |
Output is correct |
14 |
Correct |
49 ms |
82684 KB |
Output is correct |
15 |
Correct |
46 ms |
82564 KB |
Output is correct |
16 |
Correct |
20 ms |
36052 KB |
Output is correct |
17 |
Correct |
65 ms |
82736 KB |
Output is correct |
18 |
Correct |
58 ms |
82824 KB |
Output is correct |
19 |
Correct |
21 ms |
36180 KB |
Output is correct |
20 |
Correct |
49 ms |
82992 KB |
Output is correct |
21 |
Correct |
35 ms |
37268 KB |
Output is correct |
22 |
Correct |
42 ms |
38344 KB |
Output is correct |
23 |
Correct |
47 ms |
39108 KB |
Output is correct |
24 |
Correct |
82 ms |
82748 KB |
Output is correct |
25 |
Correct |
70 ms |
82820 KB |
Output is correct |
26 |
Correct |
85 ms |
83212 KB |
Output is correct |
27 |
Correct |
54 ms |
40260 KB |
Output is correct |
28 |
Correct |
126 ms |
83300 KB |
Output is correct |
29 |
Correct |
91 ms |
83268 KB |
Output is correct |
30 |
Correct |
82 ms |
40960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
35540 KB |
Output is correct |
2 |
Correct |
46 ms |
82716 KB |
Output is correct |
3 |
Correct |
49 ms |
82672 KB |
Output is correct |
4 |
Correct |
19 ms |
35540 KB |
Output is correct |
5 |
Correct |
21 ms |
35628 KB |
Output is correct |
6 |
Correct |
24 ms |
35808 KB |
Output is correct |
7 |
Correct |
43 ms |
82560 KB |
Output is correct |
8 |
Correct |
19 ms |
35760 KB |
Output is correct |
9 |
Correct |
52 ms |
82704 KB |
Output is correct |
10 |
Correct |
44 ms |
82724 KB |
Output is correct |
11 |
Correct |
392 ms |
64760 KB |
Output is correct |
12 |
Correct |
1067 ms |
115812 KB |
Output is correct |
13 |
Correct |
1250 ms |
121916 KB |
Output is correct |
14 |
Correct |
1103 ms |
91900 KB |
Output is correct |
15 |
Correct |
1128 ms |
91900 KB |
Output is correct |
16 |
Correct |
1061 ms |
90340 KB |
Output is correct |
17 |
Correct |
1104 ms |
120568 KB |
Output is correct |
18 |
Correct |
1536 ms |
131376 KB |
Output is correct |
19 |
Correct |
1653 ms |
141092 KB |
Output is correct |
20 |
Correct |
753 ms |
89660 KB |
Output is correct |
21 |
Correct |
49 ms |
82736 KB |
Output is correct |
22 |
Correct |
67 ms |
83136 KB |
Output is correct |
23 |
Correct |
51 ms |
82912 KB |
Output is correct |
24 |
Correct |
49 ms |
82684 KB |
Output is correct |
25 |
Correct |
46 ms |
82564 KB |
Output is correct |
26 |
Correct |
20 ms |
36052 KB |
Output is correct |
27 |
Correct |
65 ms |
82736 KB |
Output is correct |
28 |
Correct |
58 ms |
82824 KB |
Output is correct |
29 |
Correct |
21 ms |
36180 KB |
Output is correct |
30 |
Correct |
49 ms |
82992 KB |
Output is correct |
31 |
Correct |
35 ms |
37268 KB |
Output is correct |
32 |
Correct |
42 ms |
38344 KB |
Output is correct |
33 |
Correct |
47 ms |
39108 KB |
Output is correct |
34 |
Correct |
82 ms |
82748 KB |
Output is correct |
35 |
Correct |
70 ms |
82820 KB |
Output is correct |
36 |
Correct |
85 ms |
83212 KB |
Output is correct |
37 |
Correct |
54 ms |
40260 KB |
Output is correct |
38 |
Correct |
126 ms |
83300 KB |
Output is correct |
39 |
Correct |
91 ms |
83268 KB |
Output is correct |
40 |
Correct |
82 ms |
40960 KB |
Output is correct |
41 |
Correct |
212 ms |
51172 KB |
Output is correct |
42 |
Correct |
633 ms |
108748 KB |
Output is correct |
43 |
Correct |
275 ms |
83148 KB |
Output is correct |
44 |
Correct |
844 ms |
121484 KB |
Output is correct |
45 |
Correct |
949 ms |
111892 KB |
Output is correct |
46 |
Correct |
695 ms |
83264 KB |
Output is correct |
47 |
Correct |
934 ms |
84104 KB |
Output is correct |
48 |
Correct |
568 ms |
100072 KB |
Output is correct |
49 |
Correct |
680 ms |
80844 KB |
Output is correct |
50 |
Correct |
706 ms |
80516 KB |
Output is correct |
51 |
Correct |
296 ms |
85420 KB |
Output is correct |
52 |
Correct |
1016 ms |
112540 KB |
Output is correct |
53 |
Correct |
591 ms |
100708 KB |
Output is correct |
54 |
Correct |
1424 ms |
117404 KB |
Output is correct |
55 |
Correct |
1098 ms |
120996 KB |
Output is correct |