#include "werewolf.h"
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int NN = 2e5 + 3;
vector <int> v[NN],g[4][NN];
int p[NN],par[4][NN],d[5][25][NN],timer;
int tin[4][NN],tout[4][NN],rev[4][NN];
bool active[NN],fix[NN];
int P(int x){
if (x==p[x]) return x;
return p[x]=P(p[x]);
}
void dsu(int id,int x,int y){
if (P(x) == P(y)) return;
y = P(y);
par[id][y] = x;
g[id][x].pb(y);
// cout<<"Edge "<<y<<" -> "<<x<<endl;
p[y] = x;
}
void dfs(int id,int x,int par){
d[id][0][x] = par;
// cout<<"> "<<id<<" "<<x<<endl;
tin[id][x] = ++timer;
rev[id][timer] = x;
for (int to: g[id][x]){
if (to != par) dfs(id,to,x);
}
tout[id][x] = timer;
}
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 Q = S.size(),n = N;
vector<int> ans(Q);
for (int i=0;i<X.size();i++){
v[X[i]].pb(Y[i]);
v[Y[i]].pb(X[i]);
}
for (int i=0;i<n;i++)
p[i] = i,par[1][i] = par[2][i] = -1;
for (int i = 0; i < n; i++){
active[i] = 1;
for (int to: v[i]){
if (active[to]) dsu(1,i,to);
}
}
for (int i=0;i<n;i++)
p[i] = i,par[1][i] = par[2][i] = -1,active[i] = 0;
for (int i = n-1; i >= 0; i--){
active[i] = 1;
for (int to: v[i]){
if (active[to]) dsu(2,i,to);
}
}
int root1,root2;
for (int i=0;i<n;i++){
if (par[1][i] == -1) root1 = i;
if (par[2][i] == -1) root2 = i;
}
timer=0; dfs(1,root1,root1);
timer=0; dfs(2,root2,root2);
for (int j = 1; j < 20; j++){
for (int i=0;i<n;i++){
d[1][j][i] = d[1][j - 1][d[1][j - 1][i]];
d[2][j][i] = d[2][j - 1][d[2][j - 1][i]];
}
}
for (int i = 0; i < Q; i++){
int A = S[i],B = E[i],l = L[i],r = R[i];
for (int j = 19; j >= 0; j--) {
if (d[1][j][B] <= r) B = d[1][j][B];
if (d[2][j][A] >= l) A = d[2][j][A];
}
if (A < l || B > r) continue;
int l1 = tin[1][B],r1 = tout[1][B];
for (int j=0;j<n;j++) fix[j] = 0;
for (int j = l1; j <= r1; j++){
fix[rev[1][j]] = 1;
}
int l2 = tin[2][A],r2 = tout[2][A];
for (int j = l2; j <= r2; j++){
if (fix[rev[2][j]]) ans[i] = 1;
}
}
return ans;
}
Compilation message
werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:42:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i=0;i<X.size();i++){
| ~^~~~~~~~~
werewolf.cpp:76:15: warning: 'root2' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | timer=0; dfs(2,root2,root2);
| ~~~^~~~~~~~~~~~~~~
werewolf.cpp:75:15: warning: 'root1' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | timer=0; dfs(1,root1,root1);
| ~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24408 KB |
Output is correct |
2 |
Correct |
9 ms |
24156 KB |
Output is correct |
3 |
Correct |
10 ms |
24156 KB |
Output is correct |
4 |
Correct |
10 ms |
24156 KB |
Output is correct |
5 |
Correct |
10 ms |
24156 KB |
Output is correct |
6 |
Correct |
10 ms |
24180 KB |
Output is correct |
7 |
Correct |
10 ms |
24228 KB |
Output is correct |
8 |
Correct |
9 ms |
24156 KB |
Output is correct |
9 |
Correct |
10 ms |
24152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24408 KB |
Output is correct |
2 |
Correct |
9 ms |
24156 KB |
Output is correct |
3 |
Correct |
10 ms |
24156 KB |
Output is correct |
4 |
Correct |
10 ms |
24156 KB |
Output is correct |
5 |
Correct |
10 ms |
24156 KB |
Output is correct |
6 |
Correct |
10 ms |
24180 KB |
Output is correct |
7 |
Correct |
10 ms |
24228 KB |
Output is correct |
8 |
Correct |
9 ms |
24156 KB |
Output is correct |
9 |
Correct |
10 ms |
24152 KB |
Output is correct |
10 |
Correct |
18 ms |
25352 KB |
Output is correct |
11 |
Correct |
17 ms |
25432 KB |
Output is correct |
12 |
Correct |
13 ms |
25180 KB |
Output is correct |
13 |
Correct |
19 ms |
25436 KB |
Output is correct |
14 |
Correct |
15 ms |
25316 KB |
Output is correct |
15 |
Correct |
19 ms |
25448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
766 ms |
95812 KB |
Output is correct |
2 |
Execution timed out |
4053 ms |
101692 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24408 KB |
Output is correct |
2 |
Correct |
9 ms |
24156 KB |
Output is correct |
3 |
Correct |
10 ms |
24156 KB |
Output is correct |
4 |
Correct |
10 ms |
24156 KB |
Output is correct |
5 |
Correct |
10 ms |
24156 KB |
Output is correct |
6 |
Correct |
10 ms |
24180 KB |
Output is correct |
7 |
Correct |
10 ms |
24228 KB |
Output is correct |
8 |
Correct |
9 ms |
24156 KB |
Output is correct |
9 |
Correct |
10 ms |
24152 KB |
Output is correct |
10 |
Correct |
18 ms |
25352 KB |
Output is correct |
11 |
Correct |
17 ms |
25432 KB |
Output is correct |
12 |
Correct |
13 ms |
25180 KB |
Output is correct |
13 |
Correct |
19 ms |
25436 KB |
Output is correct |
14 |
Correct |
15 ms |
25316 KB |
Output is correct |
15 |
Correct |
19 ms |
25448 KB |
Output is correct |
16 |
Correct |
766 ms |
95812 KB |
Output is correct |
17 |
Execution timed out |
4053 ms |
101692 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |