#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
//#include "lib1270.h"
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 1000005;
int N, glob;
struct dsu {
vector<int> g, sz;
vector<short> deg;
int n, badcnt, allbad, answer;
dsu() {};
dsu (int n, int size) : n(n), answer(size), badcnt(0), allbad(0) {
g.resize(n), sz.resize(n, 1), deg.resize(n);
iota(AI(g), 0);
}
int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
void M(int a, int b) {
if (max(++deg[a], ++deg[b]) >= 3) allbad = true;
if (allbad) return;
a = F(a), b = F(b);
if (sz[a] < sz[b]) swap(a, b);
if (a != b) {
return sz[a] += sz[b], g[b] = a, void();
}
if (++badcnt == 2) allbad = true;
answer = sz[a];
}
bool noring() { return badcnt == 0 && allbad == false; }
int getans() {
return allbad ? 0 : answer;
}
};
dsu alld, forone;
vector<int> edge[MAX_N];
bool iscan[MAX_N];
vector<int> big, can;
vector<dsu> forcan;
vector<pair<int,int>> alledge;
int over = -1, lst;
void canpush(int id) { if (iscan[id]) return; iscan[id] = true, can.pb(id); }
void Init(int N_) {
for (int i = 0;i < N;++i) edge[i].clear(), iscan[i] = false, edge[i].shrink_to_fit();
big.clear(), can.clear(), forcan.clear(), alledge.clear(), over = -1, lst = 0, alld = forone = dsu();
N = N_;
glob = N;
alld = dsu(N, N);
}
void makebig() {
if (lst == 0)
forone = dsu(N, N-1);
while (lst < alledge.size()) {
auto [a, b] = alledge[lst++];
if (a != over && b != over)
forone.M(a, b);
}
if (!forone.noring()) glob = -1;
}
void superbig(int id) {
if (over == -1) {
over = id;
return;
}
else if (over != id) {
glob = -1;
return;
}
}
void adjust(int a, int b) {
if (glob == -1) return;
if (big.empty()) {
alld.M(a, b);
glob = alld.getans();
return;
}
// deal with old ones
int oldsize = can.size();
for (int i = 0;i < oldsize;++i) {
int x = can[i];
if (a != x && b != x)
forcan[i].M(a, b);
}
// put new element into candidate
for (int x : big) {
// assert(edge[x].size() <= 3);
canpush(x);
for (int u : edge[x]) canpush(u);
}
// for (int x : big) for (int y : big) if (x != y) {
// assert(edge[x].size() == 3);
// if (count(AI(edge[y]), x) == 0)
// glob = -1;
// }
// if (glob == -1) return;
assert(can.size() <= 16);
// examine new candidates
for (int i = oldsize;i < can.size();++i) {
forcan.pb(N, N-1);
int x = can[i];
for (auto [a, b] : alledge) if (a != x && b != x) {
forcan[i].M(a, b);
}
}
// assert(forcan.size() == can.size());
glob = 0;
// examine all candidates
for (int i = 0;i < can.size();++i)
glob += forcan[i].noring();
}
void Link(int A, int B) {
if (glob == -1) return;
edge[A].pb(B), edge[B].pb(A);
if (edge[A].size() == 3) big.pb(A);
if (edge[B].size() == 3) big.pb(B);
alledge.pb(A, B);
if (edge[A].size() > 3) superbig(A);
if (edge[B].size() > 3) superbig(B);
if (over != -1) return makebig(), void();
if (big.size() > 4) {
glob = -1;
return;
}
adjust(A, B);
}
int CountCritical() {
if (glob == -1) return 0;
if (over != -1) return forone.noring();
return max(glob, 0);
}
Compilation message
rings.cpp: In constructor 'dsu::dsu(int, int)':
rings.cpp:27:25: warning: 'dsu::answer' will be initialized after [-Wreorder]
27 | int n, badcnt, allbad, answer;
| ^~~~~~
rings.cpp:27:9: warning: 'int dsu::badcnt' [-Wreorder]
27 | int n, badcnt, allbad, answer;
| ^~~~~~
rings.cpp:30:2: warning: when initialized here [-Wreorder]
30 | dsu (int n, int size) : n(n), answer(size), badcnt(0), allbad(0) {
| ^~~
rings.cpp: In function 'void makebig()':
rings.cpp:87:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while (lst < alledge.size()) {
| ~~~~^~~~~~~~~~~~~~~~
rings.cpp: In function 'void adjust(int, int)':
rings.cpp:142:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for (int i = oldsize;i < can.size();++i) {
| ~~^~~~~~~~~~~~
rings.cpp:155:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | for (int i = 0;i < can.size();++i)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
19 ms |
24300 KB |
Output is correct |
3 |
Correct |
19 ms |
24428 KB |
Output is correct |
4 |
Correct |
17 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24172 KB |
Output is correct |
7 |
Correct |
18 ms |
24684 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
19 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
477 ms |
49492 KB |
Output is correct |
2 |
Correct |
1279 ms |
102216 KB |
Output is correct |
3 |
Correct |
374 ms |
162028 KB |
Output is correct |
4 |
Correct |
1223 ms |
72908 KB |
Output is correct |
5 |
Correct |
1254 ms |
72776 KB |
Output is correct |
6 |
Correct |
1193 ms |
71628 KB |
Output is correct |
7 |
Correct |
388 ms |
191212 KB |
Output is correct |
8 |
Correct |
1907 ms |
124660 KB |
Output is correct |
9 |
Correct |
2238 ms |
141144 KB |
Output is correct |
10 |
Correct |
832 ms |
71756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
19 ms |
24300 KB |
Output is correct |
3 |
Correct |
19 ms |
24428 KB |
Output is correct |
4 |
Correct |
17 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24172 KB |
Output is correct |
7 |
Correct |
18 ms |
24684 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
19 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24428 KB |
Output is correct |
11 |
Correct |
20 ms |
24428 KB |
Output is correct |
12 |
Correct |
26 ms |
25580 KB |
Output is correct |
13 |
Correct |
23 ms |
25324 KB |
Output is correct |
14 |
Correct |
20 ms |
24716 KB |
Output is correct |
15 |
Correct |
20 ms |
25324 KB |
Output is correct |
16 |
Correct |
21 ms |
24428 KB |
Output is correct |
17 |
Correct |
21 ms |
25708 KB |
Output is correct |
18 |
Correct |
22 ms |
27372 KB |
Output is correct |
19 |
Correct |
21 ms |
24556 KB |
Output is correct |
20 |
Correct |
23 ms |
25196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
19 ms |
24300 KB |
Output is correct |
3 |
Correct |
19 ms |
24428 KB |
Output is correct |
4 |
Correct |
17 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24172 KB |
Output is correct |
7 |
Correct |
18 ms |
24684 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
19 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24428 KB |
Output is correct |
11 |
Correct |
20 ms |
24428 KB |
Output is correct |
12 |
Correct |
26 ms |
25580 KB |
Output is correct |
13 |
Correct |
23 ms |
25324 KB |
Output is correct |
14 |
Correct |
20 ms |
24716 KB |
Output is correct |
15 |
Correct |
20 ms |
25324 KB |
Output is correct |
16 |
Correct |
21 ms |
24428 KB |
Output is correct |
17 |
Correct |
21 ms |
25708 KB |
Output is correct |
18 |
Correct |
22 ms |
27372 KB |
Output is correct |
19 |
Correct |
21 ms |
24556 KB |
Output is correct |
20 |
Correct |
23 ms |
25196 KB |
Output is correct |
21 |
Correct |
36 ms |
25960 KB |
Output is correct |
22 |
Correct |
50 ms |
27108 KB |
Output is correct |
23 |
Correct |
64 ms |
28040 KB |
Output is correct |
24 |
Correct |
65 ms |
31984 KB |
Output is correct |
25 |
Correct |
33 ms |
30316 KB |
Output is correct |
26 |
Correct |
64 ms |
32744 KB |
Output is correct |
27 |
Correct |
67 ms |
28640 KB |
Output is correct |
28 |
Correct |
53 ms |
39532 KB |
Output is correct |
29 |
Correct |
55 ms |
41580 KB |
Output is correct |
30 |
Correct |
84 ms |
29408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
23788 KB |
Output is correct |
2 |
Correct |
19 ms |
24300 KB |
Output is correct |
3 |
Correct |
19 ms |
24428 KB |
Output is correct |
4 |
Correct |
17 ms |
23916 KB |
Output is correct |
5 |
Correct |
18 ms |
24044 KB |
Output is correct |
6 |
Correct |
19 ms |
24172 KB |
Output is correct |
7 |
Correct |
18 ms |
24684 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
19 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24428 KB |
Output is correct |
11 |
Correct |
477 ms |
49492 KB |
Output is correct |
12 |
Correct |
1279 ms |
102216 KB |
Output is correct |
13 |
Correct |
374 ms |
162028 KB |
Output is correct |
14 |
Correct |
1223 ms |
72908 KB |
Output is correct |
15 |
Correct |
1254 ms |
72776 KB |
Output is correct |
16 |
Correct |
1193 ms |
71628 KB |
Output is correct |
17 |
Correct |
388 ms |
191212 KB |
Output is correct |
18 |
Correct |
1907 ms |
124660 KB |
Output is correct |
19 |
Correct |
2238 ms |
141144 KB |
Output is correct |
20 |
Correct |
832 ms |
71756 KB |
Output is correct |
21 |
Correct |
20 ms |
24428 KB |
Output is correct |
22 |
Correct |
26 ms |
25580 KB |
Output is correct |
23 |
Correct |
23 ms |
25324 KB |
Output is correct |
24 |
Correct |
20 ms |
24716 KB |
Output is correct |
25 |
Correct |
20 ms |
25324 KB |
Output is correct |
26 |
Correct |
21 ms |
24428 KB |
Output is correct |
27 |
Correct |
21 ms |
25708 KB |
Output is correct |
28 |
Correct |
22 ms |
27372 KB |
Output is correct |
29 |
Correct |
21 ms |
24556 KB |
Output is correct |
30 |
Correct |
23 ms |
25196 KB |
Output is correct |
31 |
Correct |
36 ms |
25960 KB |
Output is correct |
32 |
Correct |
50 ms |
27108 KB |
Output is correct |
33 |
Correct |
64 ms |
28040 KB |
Output is correct |
34 |
Correct |
65 ms |
31984 KB |
Output is correct |
35 |
Correct |
33 ms |
30316 KB |
Output is correct |
36 |
Correct |
64 ms |
32744 KB |
Output is correct |
37 |
Correct |
67 ms |
28640 KB |
Output is correct |
38 |
Correct |
53 ms |
39532 KB |
Output is correct |
39 |
Correct |
55 ms |
41580 KB |
Output is correct |
40 |
Correct |
84 ms |
29408 KB |
Output is correct |
41 |
Correct |
259 ms |
39516 KB |
Output is correct |
42 |
Correct |
807 ms |
84824 KB |
Output is correct |
43 |
Correct |
323 ms |
82988 KB |
Output is correct |
44 |
Correct |
385 ms |
188652 KB |
Output is correct |
45 |
Correct |
590 ms |
127964 KB |
Output is correct |
46 |
Correct |
811 ms |
66100 KB |
Output is correct |
47 |
Correct |
1070 ms |
66888 KB |
Output is correct |
48 |
Correct |
391 ms |
193388 KB |
Output is correct |
49 |
Correct |
780 ms |
65864 KB |
Output is correct |
50 |
Correct |
844 ms |
65628 KB |
Output is correct |
51 |
Correct |
350 ms |
77268 KB |
Output is correct |
52 |
Correct |
352 ms |
148076 KB |
Output is correct |
53 |
Correct |
354 ms |
192876 KB |
Output is correct |
54 |
Correct |
1586 ms |
120188 KB |
Output is correct |
55 |
Correct |
1164 ms |
111308 KB |
Output is correct |