#pragma GCC optimize ("O3")
#include "werewolf.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct DSU {
int components;
vector<int>par;
DSU(int n) {
par.assign(n, -1);
components = n;
}
int Find(int u) {
if (par[u] < 0) return u;
else return par[u] = Find(par[u]);
}
bool Union(int u, int v) {
u = Find(u);
v = Find(v);
if (u != v) {
par[u] += par[v];
par[v] = u;
--components;
return true;
} else {
return false;
}
}
};
void dfs1(int node, vvi &tree, int &tmr, vi &tin, vi &tout) {
tin[node] = ++tmr;
for (int to : tree[node]) {
dfs1(to, tree, tmr, tin, tout);
}
tout[node] = tmr;
}
void dfs2(int node, vvi &tree, vi &tin, vi &tout, vvpii &group, vector<set<int>> &subTree, vi &res) {
int maxChild = -1;
for (int to : tree[node]) {
dfs2(to, tree, tin, tout, group, subTree, res);
if (maxChild == -1 || size(subTree[to]) > size(subTree[maxChild])) {
maxChild = to;
}
}
if (maxChild != -1) {
swap(subTree[node], subTree[maxChild]);
///subTree[node] = subTree[maxChild];
for (int to : tree[node]) {
if (to != maxChild) {
subTree[node].insert(all(subTree[to]));
}
}
}
subTree[node].insert(tin[node]);
for (auto [v, i] : group[node]) {
auto itr = subTree[node].lower_bound(tin[v]);
if (itr != subTree[node].end() && (*itr) <= tout[v]) {
res[i] = 1;
}
}
}
vi check_validity(int N, vi X, vi Y, vi S, vi E, vi L, vi R) {
int M = size(X);
vvi g(N);
for (int i = 0; i < M; ++i) {
g[X[i]].eb(Y[i]);
g[Y[i]].eb(X[i]);
}
DSU dsu1(N), dsu2(N);
vvi treeHuman(N), treeWolf(N);
vector<vvi>up(2, vvi(19, vi(N)));
up[0][0][0] = 0;
for (int i = N - 1; i >= 0; --i) {
for (int to : g[i]) {
if (to > i && dsu1.Find(to) != i) {
treeHuman[i].eb(dsu1.Find(to));
up[0][0][dsu1.Find(to)] = i;
dsu1.Union(i, to);
}
}
}
up[1][0][N - 1] = N - 1;
for (int i = 0; i < N; ++i) {
for (int to : g[i]) {
if (to < i && dsu2.Find(to) != i) {
treeWolf[i].eb(dsu2.Find(to));
up[1][0][dsu2.Find(to)] = i;
dsu2.Union(i, to);
}
}
}
for (int k = 0; k < 2; ++k) {
for (int i = 1; i < 19; ++i) {
for (int j = 0; j < N; ++j) {
up[k][i][j] = up[k][i - 1][up[k][i - 1][j]];
}
}
}
int Q = size(S);
vvpii group(N);
for (int i = 0; i < Q; ++i) {
for (int j = 18; j >= 0; --j) {
if (up[0][j][S[i]] >= L[i]) {
S[i] = up[0][j][S[i]];
}
if (up[1][j][E[i]] <= R[i]) {
E[i] = up[1][j][E[i]];
}
}
group[E[i]].eb(mpr(S[i], i));
}
int tmr = -1;
vi tin(N), tout(N);
dfs1(0, treeHuman, tmr, tin, tout);
vi res(Q, 0);
vector<set<int>>subTree(N);
dfs2(N - 1, treeWolf, tin, tout, group, subTree, res);
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
2024 KB |
Output is correct |
11 |
Correct |
6 ms |
2004 KB |
Output is correct |
12 |
Correct |
6 ms |
2132 KB |
Output is correct |
13 |
Correct |
6 ms |
2260 KB |
Output is correct |
14 |
Correct |
6 ms |
2260 KB |
Output is correct |
15 |
Correct |
6 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
756 ms |
148468 KB |
Output is correct |
2 |
Correct |
750 ms |
122368 KB |
Output is correct |
3 |
Correct |
684 ms |
120196 KB |
Output is correct |
4 |
Correct |
689 ms |
125220 KB |
Output is correct |
5 |
Correct |
732 ms |
126548 KB |
Output is correct |
6 |
Correct |
709 ms |
133116 KB |
Output is correct |
7 |
Correct |
811 ms |
154864 KB |
Output is correct |
8 |
Correct |
746 ms |
122444 KB |
Output is correct |
9 |
Correct |
611 ms |
120036 KB |
Output is correct |
10 |
Correct |
543 ms |
124684 KB |
Output is correct |
11 |
Correct |
547 ms |
126116 KB |
Output is correct |
12 |
Correct |
551 ms |
133048 KB |
Output is correct |
13 |
Correct |
783 ms |
129500 KB |
Output is correct |
14 |
Correct |
818 ms |
129208 KB |
Output is correct |
15 |
Correct |
793 ms |
129152 KB |
Output is correct |
16 |
Correct |
810 ms |
129016 KB |
Output is correct |
17 |
Correct |
781 ms |
153600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
2024 KB |
Output is correct |
11 |
Correct |
6 ms |
2004 KB |
Output is correct |
12 |
Correct |
6 ms |
2132 KB |
Output is correct |
13 |
Correct |
6 ms |
2260 KB |
Output is correct |
14 |
Correct |
6 ms |
2260 KB |
Output is correct |
15 |
Correct |
6 ms |
2132 KB |
Output is correct |
16 |
Correct |
756 ms |
148468 KB |
Output is correct |
17 |
Correct |
750 ms |
122368 KB |
Output is correct |
18 |
Correct |
684 ms |
120196 KB |
Output is correct |
19 |
Correct |
689 ms |
125220 KB |
Output is correct |
20 |
Correct |
732 ms |
126548 KB |
Output is correct |
21 |
Correct |
709 ms |
133116 KB |
Output is correct |
22 |
Correct |
811 ms |
154864 KB |
Output is correct |
23 |
Correct |
746 ms |
122444 KB |
Output is correct |
24 |
Correct |
611 ms |
120036 KB |
Output is correct |
25 |
Correct |
543 ms |
124684 KB |
Output is correct |
26 |
Correct |
547 ms |
126116 KB |
Output is correct |
27 |
Correct |
551 ms |
133048 KB |
Output is correct |
28 |
Correct |
783 ms |
129500 KB |
Output is correct |
29 |
Correct |
818 ms |
129208 KB |
Output is correct |
30 |
Correct |
793 ms |
129152 KB |
Output is correct |
31 |
Correct |
810 ms |
129016 KB |
Output is correct |
32 |
Correct |
781 ms |
153600 KB |
Output is correct |
33 |
Correct |
815 ms |
123568 KB |
Output is correct |
34 |
Correct |
304 ms |
27656 KB |
Output is correct |
35 |
Correct |
851 ms |
123008 KB |
Output is correct |
36 |
Correct |
711 ms |
127376 KB |
Output is correct |
37 |
Correct |
966 ms |
121912 KB |
Output is correct |
38 |
Correct |
936 ms |
124976 KB |
Output is correct |
39 |
Correct |
1028 ms |
136432 KB |
Output is correct |
40 |
Correct |
949 ms |
126208 KB |
Output is correct |
41 |
Correct |
772 ms |
120200 KB |
Output is correct |
42 |
Correct |
771 ms |
125068 KB |
Output is correct |
43 |
Correct |
1084 ms |
127852 KB |
Output is correct |
44 |
Correct |
807 ms |
120964 KB |
Output is correct |
45 |
Correct |
755 ms |
137264 KB |
Output is correct |
46 |
Correct |
872 ms |
136920 KB |
Output is correct |
47 |
Correct |
906 ms |
128216 KB |
Output is correct |
48 |
Correct |
823 ms |
128072 KB |
Output is correct |
49 |
Correct |
926 ms |
128168 KB |
Output is correct |
50 |
Correct |
796 ms |
128124 KB |
Output is correct |
51 |
Correct |
735 ms |
125484 KB |
Output is correct |
52 |
Correct |
828 ms |
125456 KB |
Output is correct |