#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
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 1000005;
int N, glob;
struct dsu {
vector<int> g, sz, deg;
int n, badcnt, allbad, answer;
dsu() {};
dsu (int n, int size) : n(n), answer(size), badcnt(0), allbad(0) {
deg = g = vector<int>(n);
sz = vector<int>(n, 1);
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;
// merging into a ring
// which means you have to remove objects on the ring
// all of them is ok, since max deg is 2
// I only care when max deg is <= 2
// and a ring cannot merge with other for max deg <= 2
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;
void canpush(int id) { if (iscan[id]) return; iscan[id] = true, can.pb(id); }
void Init(int N_) {
N = N_;
glob = N;
alld = dsu(N, N);
}
vector<pair<int,int>> alledge;
// TODO big crisis
// new edge is {a, b}
int over = -1;
void superbig(int id) {
if (over == -1) {
over = id;
forone = dsu(N, N-1);
for (auto [a, b] : alledge)
if (a != id && b != id)
forone.M(a, b);
glob = forone.noring();
}
else if (over != id) {
glob = -1;
return;
}
else {
auto [a, b] = alledge.back();
if (a != id && b != id)
forone.M(a, b);
glob = forone.noring();
}
}
void adjust(int a, int b) {
if (glob == -1) return;
DE(a, b);
if (big.empty()) {
assert(can.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);
}
// 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) {
// superbig(over);
// return;
// }
if (over != -1) return;
if (big.size() > 4) {
glob = -1;
return;
}
adjust(A, B);
}
int CountCritical() {
if (over != -1) {
dsu now(N, N-1);
for (auto [a, b] : alledge)
if (a != over && b != over)
now.M(a, b);
return now.noring();
}
return max(glob, 0);
}
Compilation message
rings.cpp: In constructor 'dsu::dsu(int, int)':
rings.cpp:25:25: warning: 'dsu::answer' will be initialized after [-Wreorder]
25 | int n, badcnt, allbad, answer;
| ^~~~~~
rings.cpp:25:9: warning: 'int dsu::badcnt' [-Wreorder]
25 | int n, badcnt, allbad, answer;
| ^~~~~~
rings.cpp:28:2: warning: when initialized here [-Wreorder]
28 | dsu (int n, int size) : n(n), answer(size), badcnt(0), allbad(0) {
| ^~~
rings.cpp: In function 'void adjust(int, int)':
rings.cpp:14:17: warning: statement has no effect [-Wunused-value]
14 | #define DE(...) 0
| ^
rings.cpp:109:2: note: in expansion of macro 'DE'
109 | DE(a, b);
| ^~
rings.cpp:137:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for (int i = oldsize;i < can.size();++i) {
| ~~^~~~~~~~~~~~
rings.cpp:150:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | for (int i = 0;i < can.size();++i)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
17 ms |
24812 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
20 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
545 ms |
50260 KB |
Output is correct |
2 |
Correct |
1209 ms |
120984 KB |
Output is correct |
3 |
Correct |
406 ms |
201576 KB |
Output is correct |
4 |
Correct |
1291 ms |
74696 KB |
Output is correct |
5 |
Correct |
1296 ms |
74732 KB |
Output is correct |
6 |
Correct |
1270 ms |
73328 KB |
Output is correct |
7 |
Correct |
462 ms |
224648 KB |
Output is correct |
8 |
Correct |
2143 ms |
137344 KB |
Output is correct |
9 |
Correct |
2411 ms |
156768 KB |
Output is correct |
10 |
Correct |
901 ms |
72516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
17 ms |
24812 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
20 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24556 KB |
Output is correct |
11 |
Correct |
20 ms |
24428 KB |
Output is correct |
12 |
Correct |
27 ms |
25836 KB |
Output is correct |
13 |
Correct |
29 ms |
25580 KB |
Output is correct |
14 |
Correct |
27 ms |
24940 KB |
Output is correct |
15 |
Correct |
32 ms |
25800 KB |
Output is correct |
16 |
Correct |
28 ms |
24428 KB |
Output is correct |
17 |
Correct |
21 ms |
25836 KB |
Output is correct |
18 |
Correct |
26 ms |
27884 KB |
Output is correct |
19 |
Correct |
23 ms |
24428 KB |
Output is correct |
20 |
Correct |
23 ms |
25196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
17 ms |
24812 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
20 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24556 KB |
Output is correct |
11 |
Correct |
20 ms |
24428 KB |
Output is correct |
12 |
Correct |
27 ms |
25836 KB |
Output is correct |
13 |
Correct |
29 ms |
25580 KB |
Output is correct |
14 |
Correct |
27 ms |
24940 KB |
Output is correct |
15 |
Correct |
32 ms |
25800 KB |
Output is correct |
16 |
Correct |
28 ms |
24428 KB |
Output is correct |
17 |
Correct |
21 ms |
25836 KB |
Output is correct |
18 |
Correct |
26 ms |
27884 KB |
Output is correct |
19 |
Correct |
23 ms |
24428 KB |
Output is correct |
20 |
Correct |
23 ms |
25196 KB |
Output is correct |
21 |
Correct |
45 ms |
25704 KB |
Output is correct |
22 |
Correct |
57 ms |
26724 KB |
Output is correct |
23 |
Correct |
75 ms |
27492 KB |
Output is correct |
24 |
Execution timed out |
4025 ms |
32240 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
17 ms |
24812 KB |
Output is correct |
8 |
Correct |
18 ms |
24044 KB |
Output is correct |
9 |
Correct |
20 ms |
24428 KB |
Output is correct |
10 |
Correct |
21 ms |
24556 KB |
Output is correct |
11 |
Correct |
545 ms |
50260 KB |
Output is correct |
12 |
Correct |
1209 ms |
120984 KB |
Output is correct |
13 |
Correct |
406 ms |
201576 KB |
Output is correct |
14 |
Correct |
1291 ms |
74696 KB |
Output is correct |
15 |
Correct |
1296 ms |
74732 KB |
Output is correct |
16 |
Correct |
1270 ms |
73328 KB |
Output is correct |
17 |
Correct |
462 ms |
224648 KB |
Output is correct |
18 |
Correct |
2143 ms |
137344 KB |
Output is correct |
19 |
Correct |
2411 ms |
156768 KB |
Output is correct |
20 |
Correct |
901 ms |
72516 KB |
Output is correct |
21 |
Correct |
20 ms |
24428 KB |
Output is correct |
22 |
Correct |
27 ms |
25836 KB |
Output is correct |
23 |
Correct |
29 ms |
25580 KB |
Output is correct |
24 |
Correct |
27 ms |
24940 KB |
Output is correct |
25 |
Correct |
32 ms |
25800 KB |
Output is correct |
26 |
Correct |
28 ms |
24428 KB |
Output is correct |
27 |
Correct |
21 ms |
25836 KB |
Output is correct |
28 |
Correct |
26 ms |
27884 KB |
Output is correct |
29 |
Correct |
23 ms |
24428 KB |
Output is correct |
30 |
Correct |
23 ms |
25196 KB |
Output is correct |
31 |
Correct |
45 ms |
25704 KB |
Output is correct |
32 |
Correct |
57 ms |
26724 KB |
Output is correct |
33 |
Correct |
75 ms |
27492 KB |
Output is correct |
34 |
Execution timed out |
4025 ms |
32240 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |