#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> adj[202020];
struct disjoint {
int root[202020], num[202020];
int r, mn[404040], mx[404040], par[404040];
vector<int> child[404040];
int _find(int v) {
if (v == root[v]) return v;
else return root[v] = _find(root[v]);
}
void _union(int v, int u) {
v = _find(v);
u = _find(u);
if (v == u) return;
if (v > u) swap(u, v);
root[u] = v;
child[r].push_back(num[v]);
child[r].push_back(num[u]);
par[num[v]] = par[num[u]] = r;
mn[r] = min(mn[num[v]], mn[num[u]]);
mx[r] = max(mx[num[v]], mx[num[u]]);
num[v] = r;
r++;
}
int ord[808080], lt[404040], rt[404040], s;
void ett() {
s = 0;
DFS(r - 1);
}
void DFS(int v) {
ord[++s] = v;
lt[v] = s;
for (int i = 0; i < child[v].size(); i++) {
int next = child[v][i];
DFS(next);
ord[++s] = v;
}
rt[v] = s;
}
int table[404040][20];
void sparse() {
for (int i = 0; i < r; i++) table[i][0] = par[i];
for (int k = 1; k < 20; k++) {
for (int i = 0; i < r; i++) {
if (table[i][k - 1] < 0) table[i][k] = -1;
else table[i][k] = table[table[i][k - 1]][k - 1];
}
}
/*
for (int i = 0; i < r; i++) {
for (int k = 0; k < 20; k++) cout << table[i][k] << " ";
cout << "\n";
}
cout << "\n";
*/
}
pair<int, int> get_segment(int st, int limit) {
if (st > limit) {
int now = st;
for (int k = 19; k >= 0; k--) {
if (table[now][k] != -1 && mn[table[now][k]] >= limit) now = table[now][k];
}
return {lt[now], rt[now]};
}
if (st < limit) {
int now = st;
for (int k = 19; k >= 0; k--) {
if (table[now][k] != -1 && mx[table[now][k]] <= limit) now = table[now][k];
}
return {lt[now], rt[now]};
}
if (st == limit) {
return {lt[st], rt[st]};
}
}
} left_tree, right_tree;
void make_left_tree() {
for (int i = 0; i < n; i++) left_tree.root[i] = i;
for (int i = 0; i < n; i++) left_tree.num[i] = i;
for (int i = 0; i < n; i++) left_tree.mn[i] = left_tree.mx[i] = i;
left_tree.r = n;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < adj[i].size(); j++) {
int v = adj[i][j];
if (v >= i) left_tree._union(i, v);
}
}
left_tree.par[left_tree.r - 1] = -1;
}
void make_right_tree() {
for (int i = 0; i < n; i++) right_tree.root[i] = i;
for (int i = 0; i < n; i++) right_tree.num[i] = i;
for (int i = 0; i < n; i++) right_tree.mn[i] = right_tree.mx[i] = i;
right_tree.r = n;
for (int i = 0; i <= n - 1; i++) {
for (int j = 0; j < adj[i].size(); j++) {
int v = adj[i][j];
if (v <= i) right_tree._union(i, v);
}
}
right_tree.par[right_tree.r - 1] = -1;
}
vector<int> ans;
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) {
n = N; m = X.size();
for (int i = 0; i < m; i++) {
adj[X[i]].push_back(Y[i]);
adj[Y[i]].push_back(X[i]);
}
make_left_tree();
left_tree.ett();
left_tree.sparse();
make_right_tree();
right_tree.ett();
right_tree.sparse();
q = S.size();
for (int i = 0; i < q; i++) ans.push_back(0);
for (int i = 0; i < q; i++) {
int s = S[i], e = E[i], l = L[i], r = R[i];
pair<int, int> left_segment = left_tree.get_segment(s, l);
//cout << left_segment.first << " " << left_segment.second << "\n";
pair<int, int> right_segment = right_tree.get_segment(e, r);
//cout << right_segment.first << " " << right_segment.second << "\n";
int ch[3030];
for (int j = 0; j < n; j++) ch[j] = 0;
for (int j = left_segment.first; j <= left_segment.second; j++)
if (left_tree.ord[j] < n) ch[left_tree.ord[j]] = 1;
for (int j = right_segment.first; j <= right_segment.second; j++)
if (right_tree.ord[j] < n && ch[right_tree.ord[j]] == 1) ans[i] = 1;
}
return ans;
}
Compilation message
werewolf.cpp: In member function 'void disjoint::DFS(int)':
werewolf.cpp:38:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i = 0; i < child[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
werewolf.cpp: In function 'void make_left_tree()':
werewolf.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j = 0; j < adj[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
werewolf.cpp: In function 'void make_right_tree()':
werewolf.cpp:106:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int j = 0; j < adj[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
werewolf.cpp: In member function 'std::pair<int, int> disjoint::get_segment(int, int)':
werewolf.cpp:82:5: warning: control reaches end of non-void function [-Wreturn-type]
82 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
24148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
24148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
558 ms |
286192 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
24148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |