#include "werewolf.h"
#include "iostream"
#include "array"
#include "queue"
#include "bitset"
#include "algorithm"
#ifndef EVAL
#include "grader.cpp"
#endif
using namespace std;
using i64 = long long;
constexpr int B = 100;
constexpr int NB = 200000 / B;
struct DSUmin {
int n;
vector<int> p;
DSUmin(int n) : n(n), p(n) { for (int i = 0; i < n; i++) p[i] = i; }
int leader(int u) {
while (u != p[u]) u = p[u] = p[p[u]];
return u;
}
void merge(int u, int v) {
u = leader(u);
v = leader(v);
if (u == v) return;
p[v] = u;
}
};
struct DSUbit {
int n;
vector<int> p, siz;
vector<bitset<NB>> f;
DSUbit(int n) : n(n), p(n), siz(n, 1), f(n) { for (int i = 0; i < n; i++) p[i] = i; }
int leader(int u) {
while (u != p[u]) u = p[u] = p[p[u]];
return u;
}
void merge(int u, int v) {
u = leader(u);
v = leader(v);
if (u == v) return;
if (siz[v] > siz[u]) swap(u, v);
f[u] |= f[v];
siz[u] += siz[v];
p[v] = u;
}
};
std::vector<int> Min;
std::vector<int> block;
struct DSU {
int n;
vector<int> p, siz;
vector<vector<int>> f;
vector<bitset<NB>> bf;
DSU(int n) : n(n), p(n), siz(n), f(n), bf(n) {
for (int i = 0; i < n; i++) {
siz[i] = 1;
p[i] = i;
f[i].push_back(i);
}
}
int leader(int u) {
while (u != p[u]) u = p[u] = p[p[u]];
return u;
}
void merge(int u, int v) {
u = leader(u);
v = leader(v);
if (u == v) return;
if (siz[v] > siz[u]) swap(u, v);
for (int x : f[v]) {
f[u].push_back(x);
}
f[v].clear();
f[v].shrink_to_fit();
siz[u] += siz[v];
bf[u] |= bf[v];
p[v] = u;
if (f[u].size() >= B) {
for (int i = 0; i < B; i++) {
block[f[u].back()] = Min.size();
f[u].pop_back();
}
bf[u][Min.size()] = 1;
Min.push_back(n);
}
}
};
std::vector<int> check_validity(int n, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E,
std::vector<int> L, std::vector<int> R) {
int m = X.size();
vector<vector<int>> e(n);
for (int i = 0; i < m; i++) {
e[X[i]].push_back(Y[i]);
e[Y[i]].push_back(X[i]);
}
int Q = S.size();
vector<int> ans(Q);
vector<vector<array<int, 4>>> q(n);
for (int i = 0; i < Q; i++) {
q[R[i]].push_back({i, S[i], E[i], L[i]});
}
DSU dsu(n);
block.assign(n, -1);
vector<vector<array<int, 3>>> nq(n);
vector<vector<tuple<int, int, bitset<NB>>>> bq(n);
for (int i = 0; i < n; i++) {
for (int j : e[i]) {
if (j < i) {
dsu.merge(i, j);
}
}
for (auto [id, s, t, L] : q[i]) {
for (int x : dsu.f[dsu.leader(t)]) {
nq[L].push_back({id, s, x});
}
bq[L].push_back({id, s, dsu.bf[dsu.leader(t)]});
}
}
DSUmin fastdsu(n);
DSUbit bitdsu(n);
for (int i = 0; i < n; i++) {
if (block[i] != -1) {
bitdsu.f[i][block[i]] = 1;
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j : e[i]) {
if (j > i) {
fastdsu.merge(i, j);
bitdsu.merge(i, j);
}
}
for (auto [id, s, x] : nq[i]) {
if (fastdsu.leader(x) == fastdsu.leader(s)) {
ans[id] = 1;
}
}
for (auto [id, s, x] : bq[i]) {
if ((bitdsu.f[bitdsu.leader(s)] & x).count()) {
ans[id] = 1;
}
}
}
return ans;
}
/*
6 6 3
5 1
1 2
1 3
3 4
3 0
5 2
4 2 1 2
4 2 2 2
5 4 3 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
7 ms |
5460 KB |
Output is correct |
11 |
Correct |
9 ms |
5176 KB |
Output is correct |
12 |
Correct |
7 ms |
4564 KB |
Output is correct |
13 |
Correct |
8 ms |
6292 KB |
Output is correct |
14 |
Correct |
8 ms |
6228 KB |
Output is correct |
15 |
Correct |
7 ms |
5360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
713 ms |
278632 KB |
Output is correct |
2 |
Correct |
712 ms |
385628 KB |
Output is correct |
3 |
Correct |
709 ms |
385836 KB |
Output is correct |
4 |
Correct |
721 ms |
381720 KB |
Output is correct |
5 |
Correct |
735 ms |
368952 KB |
Output is correct |
6 |
Correct |
779 ms |
340972 KB |
Output is correct |
7 |
Correct |
614 ms |
283504 KB |
Output is correct |
8 |
Correct |
753 ms |
384916 KB |
Output is correct |
9 |
Correct |
545 ms |
344796 KB |
Output is correct |
10 |
Correct |
634 ms |
366104 KB |
Output is correct |
11 |
Correct |
595 ms |
294228 KB |
Output is correct |
12 |
Correct |
749 ms |
342360 KB |
Output is correct |
13 |
Correct |
738 ms |
385088 KB |
Output is correct |
14 |
Correct |
741 ms |
383812 KB |
Output is correct |
15 |
Correct |
728 ms |
384960 KB |
Output is correct |
16 |
Correct |
750 ms |
384596 KB |
Output is correct |
17 |
Correct |
675 ms |
303092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
7 ms |
5460 KB |
Output is correct |
11 |
Correct |
9 ms |
5176 KB |
Output is correct |
12 |
Correct |
7 ms |
4564 KB |
Output is correct |
13 |
Correct |
8 ms |
6292 KB |
Output is correct |
14 |
Correct |
8 ms |
6228 KB |
Output is correct |
15 |
Correct |
7 ms |
5360 KB |
Output is correct |
16 |
Correct |
713 ms |
278632 KB |
Output is correct |
17 |
Correct |
712 ms |
385628 KB |
Output is correct |
18 |
Correct |
709 ms |
385836 KB |
Output is correct |
19 |
Correct |
721 ms |
381720 KB |
Output is correct |
20 |
Correct |
735 ms |
368952 KB |
Output is correct |
21 |
Correct |
779 ms |
340972 KB |
Output is correct |
22 |
Correct |
614 ms |
283504 KB |
Output is correct |
23 |
Correct |
753 ms |
384916 KB |
Output is correct |
24 |
Correct |
545 ms |
344796 KB |
Output is correct |
25 |
Correct |
634 ms |
366104 KB |
Output is correct |
26 |
Correct |
595 ms |
294228 KB |
Output is correct |
27 |
Correct |
749 ms |
342360 KB |
Output is correct |
28 |
Correct |
738 ms |
385088 KB |
Output is correct |
29 |
Correct |
741 ms |
383812 KB |
Output is correct |
30 |
Correct |
728 ms |
384960 KB |
Output is correct |
31 |
Correct |
750 ms |
384596 KB |
Output is correct |
32 |
Correct |
675 ms |
303092 KB |
Output is correct |
33 |
Correct |
739 ms |
313312 KB |
Output is correct |
34 |
Correct |
462 ms |
242860 KB |
Output is correct |
35 |
Correct |
782 ms |
332920 KB |
Output is correct |
36 |
Correct |
767 ms |
289140 KB |
Output is correct |
37 |
Correct |
746 ms |
324984 KB |
Output is correct |
38 |
Correct |
720 ms |
299524 KB |
Output is correct |
39 |
Correct |
756 ms |
384456 KB |
Output is correct |
40 |
Correct |
790 ms |
391464 KB |
Output is correct |
41 |
Correct |
703 ms |
309424 KB |
Output is correct |
42 |
Correct |
500 ms |
239352 KB |
Output is correct |
43 |
Correct |
811 ms |
372164 KB |
Output is correct |
44 |
Correct |
736 ms |
324504 KB |
Output is correct |
45 |
Correct |
623 ms |
383968 KB |
Output is correct |
46 |
Correct |
587 ms |
360592 KB |
Output is correct |
47 |
Correct |
720 ms |
384484 KB |
Output is correct |
48 |
Correct |
727 ms |
384044 KB |
Output is correct |
49 |
Correct |
713 ms |
384144 KB |
Output is correct |
50 |
Correct |
799 ms |
384844 KB |
Output is correct |
51 |
Correct |
652 ms |
347672 KB |
Output is correct |
52 |
Correct |
700 ms |
351420 KB |
Output is correct |