#include <bits/stdc++.h>
#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAX_N = 200000;
const int MAX_M = 200000;
int N, M;
vector<int> gp[MAX_N+1];
int num[MAX_N+1][2];
int lv[MAX_N+1];
int cnt = -1;
int p[MAX_N+1][20];
struct SEG{
SEG (int l, int r, int mx, int lazy) : l(l), r(r), mx(mx), lazy(lazy) {}
int l, r, mx, lazy;
};
vector<SEG> seg;
void init(int idx, int s, int e){
seg.push_back({-1, -1, 1, 0});
if(s==e) return;
seg[idx].l = seg.size(); init(seg[idx].l, s, (s+e)/2);
seg[idx].r = seg.size(); init(seg[idx].r, (s+e)/2+1, e);
}
void lazy2(int idx, int s, int e, int x, int y, int lz){
if(idx==-1) return;
if(x<=s && e<=y){
seg[idx].lazy = max(lz, seg[idx].lazy); seg[idx].mx = max(seg[idx].mx, lz);
return;
}if(x>e || y<s) return;
lazy2(seg[idx].l, s, (s+e)/2, x, y, lz);
lazy2(seg[idx].r, (s+e)/2+1, e, x, y, lz);
}
void lazy(int x, int y, int z){
lazy2(0, 0, N-1, x, y, z);
}
int find2(int idx, int s, int e, int k){
if(seg[idx].lazy!=0){
if(seg[idx].l!=-1){
seg[seg[idx].l].mx = max(seg[seg[idx].l].mx, seg[idx].lazy); seg[seg[idx].l].lazy = max(seg[seg[idx].l].lazy, seg[idx].lazy);
}if(seg[idx].r!=-1){
seg[seg[idx].r].mx = max(seg[seg[idx].r].mx, seg[idx].lazy); seg[seg[idx].r].lazy = max(seg[seg[idx].r].lazy, seg[idx].lazy);
}seg[idx].lazy = 0;
}
if(s==e) return seg[idx].mx;
if(k<=(s+e)/2) return find2(seg[idx].l, s, (s+e)/2, k);
else return find2(seg[idx].r, (s+e)/2+1, e, k);
}
int find(int x){
return find2(0, 0, N-1, x);
}
int up(int x, int y){
for(int k=19; k>=0; k--){
if(lv[p[x][k]]>=y) x = p[x][k];
}
return x;
}
void dfs(int x){
for(int k=1; k<20; k++) p[x][k] = p[p[x][k-1]][k-1];
num[x][0] = ++cnt;
lv[x] = lv[p[x][0]]+1;
for(auto i : gp[x]) dfs(i);
num[x][1] = cnt;
}
bool query(int x, int y){
int lx = find(num[x][0]), ly = find(num[y][0]);
x = up(x, lx), y = up(y, ly);
if(x==y) return true;
return false;
}
void cut(int x){
lazy(num[x][0], num[x][1], lv[x]);
}
int main(){
scanf("%d%d", &N, &M);
init(0, 0, N-1);
for(int i=2; i<=N; i++){
scanf("%d", &p[i][0]); gp[p[i][0]].pb(i);
}
dfs(1);
for(int i=0; i<M; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
bool tf = query(a, b);
if(tf){
printf("YES\n");
if(c) cut(a);
}else{
printf("NO\n");
if(c) cut(b);
}
}
return 0;
}
Compilation message
tree.cpp: In function 'int main()':
tree.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
tree.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &p[i][0]); gp[p[i][0]].pb(i);
~~~~~^~~~~~~~~~~~~~~~
tree.cpp:115:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5248 KB |
Output is correct |
2 |
Correct |
9 ms |
5220 KB |
Output is correct |
3 |
Correct |
7 ms |
5220 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
5220 KB |
Output is correct |
6 |
Correct |
10 ms |
5248 KB |
Output is correct |
7 |
Correct |
8 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5220 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5248 KB |
Output is correct |
2 |
Correct |
9 ms |
5220 KB |
Output is correct |
3 |
Correct |
7 ms |
5220 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
5220 KB |
Output is correct |
6 |
Correct |
10 ms |
5248 KB |
Output is correct |
7 |
Correct |
8 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5220 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
10 ms |
5120 KB |
Output is correct |
14 |
Correct |
9 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
10 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5248 KB |
Output is correct |
2 |
Correct |
9 ms |
5220 KB |
Output is correct |
3 |
Correct |
7 ms |
5220 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
5220 KB |
Output is correct |
6 |
Correct |
10 ms |
5248 KB |
Output is correct |
7 |
Correct |
8 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5220 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
10 ms |
5120 KB |
Output is correct |
14 |
Correct |
9 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
10 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
221 ms |
7856 KB |
Output is correct |
23 |
Correct |
269 ms |
7904 KB |
Output is correct |
24 |
Correct |
197 ms |
7800 KB |
Output is correct |
25 |
Correct |
183 ms |
7880 KB |
Output is correct |
26 |
Correct |
186 ms |
7868 KB |
Output is correct |
27 |
Correct |
210 ms |
8056 KB |
Output is correct |
28 |
Correct |
215 ms |
7980 KB |
Output is correct |
29 |
Correct |
224 ms |
7880 KB |
Output is correct |
30 |
Correct |
166 ms |
8056 KB |
Output is correct |
31 |
Correct |
158 ms |
7928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5248 KB |
Output is correct |
2 |
Correct |
9 ms |
5220 KB |
Output is correct |
3 |
Correct |
7 ms |
5220 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
5220 KB |
Output is correct |
6 |
Correct |
10 ms |
5248 KB |
Output is correct |
7 |
Correct |
8 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5220 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5248 KB |
Output is correct |
11 |
Correct |
1060 ms |
44112 KB |
Output is correct |
12 |
Correct |
1227 ms |
44184 KB |
Output is correct |
13 |
Correct |
1193 ms |
44240 KB |
Output is correct |
14 |
Correct |
1285 ms |
44212 KB |
Output is correct |
15 |
Correct |
1315 ms |
44296 KB |
Output is correct |
16 |
Correct |
1110 ms |
44212 KB |
Output is correct |
17 |
Correct |
836 ms |
44404 KB |
Output is correct |
18 |
Correct |
798 ms |
44240 KB |
Output is correct |
19 |
Correct |
758 ms |
44496 KB |
Output is correct |
20 |
Correct |
845 ms |
44392 KB |
Output is correct |
21 |
Correct |
952 ms |
44248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5248 KB |
Output is correct |
2 |
Correct |
9 ms |
5220 KB |
Output is correct |
3 |
Correct |
7 ms |
5220 KB |
Output is correct |
4 |
Correct |
9 ms |
5248 KB |
Output is correct |
5 |
Correct |
8 ms |
5220 KB |
Output is correct |
6 |
Correct |
10 ms |
5248 KB |
Output is correct |
7 |
Correct |
8 ms |
5248 KB |
Output is correct |
8 |
Correct |
7 ms |
5220 KB |
Output is correct |
9 |
Correct |
8 ms |
5248 KB |
Output is correct |
10 |
Correct |
9 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
10 ms |
5120 KB |
Output is correct |
14 |
Correct |
9 ms |
5120 KB |
Output is correct |
15 |
Correct |
9 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
7 ms |
5120 KB |
Output is correct |
18 |
Correct |
10 ms |
5120 KB |
Output is correct |
19 |
Correct |
8 ms |
5120 KB |
Output is correct |
20 |
Correct |
8 ms |
5120 KB |
Output is correct |
21 |
Correct |
8 ms |
5120 KB |
Output is correct |
22 |
Correct |
221 ms |
7856 KB |
Output is correct |
23 |
Correct |
269 ms |
7904 KB |
Output is correct |
24 |
Correct |
197 ms |
7800 KB |
Output is correct |
25 |
Correct |
183 ms |
7880 KB |
Output is correct |
26 |
Correct |
186 ms |
7868 KB |
Output is correct |
27 |
Correct |
210 ms |
8056 KB |
Output is correct |
28 |
Correct |
215 ms |
7980 KB |
Output is correct |
29 |
Correct |
224 ms |
7880 KB |
Output is correct |
30 |
Correct |
166 ms |
8056 KB |
Output is correct |
31 |
Correct |
158 ms |
7928 KB |
Output is correct |
32 |
Correct |
1060 ms |
44112 KB |
Output is correct |
33 |
Correct |
1227 ms |
44184 KB |
Output is correct |
34 |
Correct |
1193 ms |
44240 KB |
Output is correct |
35 |
Correct |
1285 ms |
44212 KB |
Output is correct |
36 |
Correct |
1315 ms |
44296 KB |
Output is correct |
37 |
Correct |
1110 ms |
44212 KB |
Output is correct |
38 |
Correct |
836 ms |
44404 KB |
Output is correct |
39 |
Correct |
798 ms |
44240 KB |
Output is correct |
40 |
Correct |
758 ms |
44496 KB |
Output is correct |
41 |
Correct |
845 ms |
44392 KB |
Output is correct |
42 |
Correct |
952 ms |
44248 KB |
Output is correct |
43 |
Correct |
735 ms |
33628 KB |
Output is correct |
44 |
Correct |
619 ms |
33616 KB |
Output is correct |
45 |
Correct |
689 ms |
33520 KB |
Output is correct |
46 |
Correct |
649 ms |
33736 KB |
Output is correct |
47 |
Correct |
631 ms |
33620 KB |
Output is correct |
48 |
Correct |
467 ms |
33616 KB |
Output is correct |
49 |
Correct |
427 ms |
33132 KB |
Output is correct |
50 |
Correct |
409 ms |
33116 KB |
Output is correct |
51 |
Correct |
722 ms |
39888 KB |
Output is correct |