#include<stdio.h>
#include<memory.h>
#include<vector>
#include<algorithm>
#define SIZE 524288
using namespace std;
struct seg_tree
{
int limit[SIZE+2], base;
void setup(int a)
{
base=1;
while(base<a) base*=2;
for(int i=1 ; i<=base*2 ; i++) limit[i]=1;
base--;
}
void propagate(int ns, int nf, int num)
{
if(limit[num]!=1)
{
if(ns<nf)
{
limit[num*2]=max(limit[num*2],limit[num]);
limit[num*2+1]=max(limit[num*2+1],limit[num]);
}
}
}
int get_limit(int st, int fn, int ns, int nf, int num)
{
propagate(ns,nf,num);
if(ns>fn || nf<st) return 1;
if(st<=ns && nf<=fn) return limit[num];
int mid=(ns+nf)/2;
return max(get_limit(st,fn,ns,mid,num*2),get_limit(st,fn,mid+1,nf,num*2+1));
}
void update(int st ,int fn, int ns, int nf, int num, int val)
{
propagate(ns,nf,num);
if(ns>fn || nf<st) return;
if(st<=ns && nf<=fn)
{
limit[num]=max(limit[num],val);
propagate(ns,nf,num);
return;
}
int mid=(ns+nf)/2;
update(st,fn,ns,mid,num*2,val);
update(st,fn,mid+1,nf,num*2+1,val);
}
};
int n, qn;
vector<int> lst[200002];
int depth[200002], par[200002][20];
int dfsn[200002], cnt, enode[200002]; // dfs-order renumbering
seg_tree T;
void dfs(int v, int d)
{
dfsn[v]=++cnt;
depth[v]=d;
for(int i=0 ; i<lst[v].size() ; i++)
{
int nxt=lst[v][i];
if(!depth[nxt])
{
par[nxt][0]=v;
dfs(nxt,d+1);
}
}
enode[v]=cnt;
}
bool pos(int b, int c)
{
if(depth[b]<depth[c]) swap(b,c);
int gap=depth[b]-depth[c], idx=0;
while(gap!=0)
{
if(gap%2==1)
{
int limit=T.get_limit(dfsn[b],dfsn[b],1,T.base+1,1);
b=par[b][idx];
if(limit>depth[b]) return false;
}
gap/=2, idx++;
}
if(b==c) return true;
for(int i=19 ; i>=0 ; i--)
{
if(par[b][i]!=par[c][i])
{
int limit_b=T.get_limit(dfsn[b],dfsn[b],1,T.base+1,1);
b=par[b][i];
int limit_c=T.get_limit(dfsn[c],dfsn[c],1,T.base+1,1);
c=par[c][i];
if(limit_b>depth[b] || limit_c>depth[c]) return false;
}
}
int limit_b=T.get_limit(dfsn[b],dfsn[b],1,T.base+1,1);
b=par[b][0];
int limit_c=T.get_limit(dfsn[c],dfsn[c],1,T.base+1,1);
c=par[c][0];
if(limit_b>depth[b] || limit_c>depth[c]) return false;
return true;
}
int main()
{
scanf("%d %d",&n,&qn);
for(int i=2 ; i<=n ; i++)
{
int x;
scanf("%d",&x);
lst[x].push_back(i);
lst[i].push_back(x);
}
T.setup(n);
memset(par,-1,sizeof(par));
dfs(1,1);
for(int j=1 ; j<20 ; j++)
{
for(int i=1 ; i<=n ; i++) if(par[i][j-1]!=-1) par[i][j]=par[par[i][j-1]][j-1];
}
for(int i=0 ; i<qn ; i++)
{
int b, c, d;
scanf("%d %d %d",&b,&c,&d);
bool tf=pos(b,c);
printf("%s\n",tf ? "YES" : "NO");
if(d==1)
{
if(tf) T.update(dfsn[b],enode[b],1,T.base+1,1,depth[b]);
else T.update(dfsn[c],enode[c],1,T.base+1,1,depth[c]);
}
}
return 0;
}
Compilation message
tree.cpp: In function 'void dfs(int, int)':
tree.cpp:64:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0 ; i<lst[v].size() ; i++)
~^~~~~~~~~~~~~~
tree.cpp: In function 'int main()':
tree.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&qn);
~~~~~^~~~~~~~~~~~~~~~
tree.cpp:116:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x);
~~~~~^~~~~~~~~
tree.cpp:130:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&b,&c,&d);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
20856 KB |
Output is correct |
2 |
Correct |
25 ms |
20992 KB |
Output is correct |
3 |
Correct |
20 ms |
20992 KB |
Output is correct |
4 |
Correct |
25 ms |
20992 KB |
Output is correct |
5 |
Correct |
25 ms |
21036 KB |
Output is correct |
6 |
Correct |
22 ms |
21036 KB |
Output is correct |
7 |
Correct |
20 ms |
21036 KB |
Output is correct |
8 |
Correct |
20 ms |
21036 KB |
Output is correct |
9 |
Correct |
19 ms |
21100 KB |
Output is correct |
10 |
Correct |
20 ms |
21172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
20856 KB |
Output is correct |
2 |
Correct |
25 ms |
20992 KB |
Output is correct |
3 |
Correct |
20 ms |
20992 KB |
Output is correct |
4 |
Correct |
25 ms |
20992 KB |
Output is correct |
5 |
Correct |
25 ms |
21036 KB |
Output is correct |
6 |
Correct |
22 ms |
21036 KB |
Output is correct |
7 |
Correct |
20 ms |
21036 KB |
Output is correct |
8 |
Correct |
20 ms |
21036 KB |
Output is correct |
9 |
Correct |
19 ms |
21100 KB |
Output is correct |
10 |
Correct |
20 ms |
21172 KB |
Output is correct |
11 |
Correct |
20 ms |
21172 KB |
Output is correct |
12 |
Correct |
21 ms |
21172 KB |
Output is correct |
13 |
Correct |
20 ms |
21172 KB |
Output is correct |
14 |
Correct |
20 ms |
21180 KB |
Output is correct |
15 |
Correct |
19 ms |
21212 KB |
Output is correct |
16 |
Correct |
20 ms |
21228 KB |
Output is correct |
17 |
Correct |
21 ms |
21244 KB |
Output is correct |
18 |
Correct |
19 ms |
21260 KB |
Output is correct |
19 |
Correct |
20 ms |
21276 KB |
Output is correct |
20 |
Correct |
20 ms |
21292 KB |
Output is correct |
21 |
Correct |
25 ms |
21376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
20856 KB |
Output is correct |
2 |
Correct |
25 ms |
20992 KB |
Output is correct |
3 |
Correct |
20 ms |
20992 KB |
Output is correct |
4 |
Correct |
25 ms |
20992 KB |
Output is correct |
5 |
Correct |
25 ms |
21036 KB |
Output is correct |
6 |
Correct |
22 ms |
21036 KB |
Output is correct |
7 |
Correct |
20 ms |
21036 KB |
Output is correct |
8 |
Correct |
20 ms |
21036 KB |
Output is correct |
9 |
Correct |
19 ms |
21100 KB |
Output is correct |
10 |
Correct |
20 ms |
21172 KB |
Output is correct |
11 |
Correct |
20 ms |
21172 KB |
Output is correct |
12 |
Correct |
21 ms |
21172 KB |
Output is correct |
13 |
Correct |
20 ms |
21172 KB |
Output is correct |
14 |
Correct |
20 ms |
21180 KB |
Output is correct |
15 |
Correct |
19 ms |
21212 KB |
Output is correct |
16 |
Correct |
20 ms |
21228 KB |
Output is correct |
17 |
Correct |
21 ms |
21244 KB |
Output is correct |
18 |
Correct |
19 ms |
21260 KB |
Output is correct |
19 |
Correct |
20 ms |
21276 KB |
Output is correct |
20 |
Correct |
20 ms |
21292 KB |
Output is correct |
21 |
Correct |
25 ms |
21376 KB |
Output is correct |
22 |
Correct |
183 ms |
24256 KB |
Output is correct |
23 |
Correct |
179 ms |
26536 KB |
Output is correct |
24 |
Correct |
170 ms |
28748 KB |
Output is correct |
25 |
Correct |
175 ms |
29940 KB |
Output is correct |
26 |
Correct |
178 ms |
29964 KB |
Output is correct |
27 |
Correct |
226 ms |
30108 KB |
Output is correct |
28 |
Correct |
259 ms |
30108 KB |
Output is correct |
29 |
Correct |
222 ms |
30108 KB |
Output is correct |
30 |
Correct |
322 ms |
30140 KB |
Output is correct |
31 |
Correct |
271 ms |
30140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
20856 KB |
Output is correct |
2 |
Correct |
25 ms |
20992 KB |
Output is correct |
3 |
Correct |
20 ms |
20992 KB |
Output is correct |
4 |
Correct |
25 ms |
20992 KB |
Output is correct |
5 |
Correct |
25 ms |
21036 KB |
Output is correct |
6 |
Correct |
22 ms |
21036 KB |
Output is correct |
7 |
Correct |
20 ms |
21036 KB |
Output is correct |
8 |
Correct |
20 ms |
21036 KB |
Output is correct |
9 |
Correct |
19 ms |
21100 KB |
Output is correct |
10 |
Correct |
20 ms |
21172 KB |
Output is correct |
11 |
Correct |
522 ms |
49832 KB |
Output is correct |
12 |
Correct |
489 ms |
54072 KB |
Output is correct |
13 |
Correct |
495 ms |
58308 KB |
Output is correct |
14 |
Correct |
495 ms |
62560 KB |
Output is correct |
15 |
Runtime error |
489 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
20856 KB |
Output is correct |
2 |
Correct |
25 ms |
20992 KB |
Output is correct |
3 |
Correct |
20 ms |
20992 KB |
Output is correct |
4 |
Correct |
25 ms |
20992 KB |
Output is correct |
5 |
Correct |
25 ms |
21036 KB |
Output is correct |
6 |
Correct |
22 ms |
21036 KB |
Output is correct |
7 |
Correct |
20 ms |
21036 KB |
Output is correct |
8 |
Correct |
20 ms |
21036 KB |
Output is correct |
9 |
Correct |
19 ms |
21100 KB |
Output is correct |
10 |
Correct |
20 ms |
21172 KB |
Output is correct |
11 |
Correct |
20 ms |
21172 KB |
Output is correct |
12 |
Correct |
21 ms |
21172 KB |
Output is correct |
13 |
Correct |
20 ms |
21172 KB |
Output is correct |
14 |
Correct |
20 ms |
21180 KB |
Output is correct |
15 |
Correct |
19 ms |
21212 KB |
Output is correct |
16 |
Correct |
20 ms |
21228 KB |
Output is correct |
17 |
Correct |
21 ms |
21244 KB |
Output is correct |
18 |
Correct |
19 ms |
21260 KB |
Output is correct |
19 |
Correct |
20 ms |
21276 KB |
Output is correct |
20 |
Correct |
20 ms |
21292 KB |
Output is correct |
21 |
Correct |
25 ms |
21376 KB |
Output is correct |
22 |
Correct |
183 ms |
24256 KB |
Output is correct |
23 |
Correct |
179 ms |
26536 KB |
Output is correct |
24 |
Correct |
170 ms |
28748 KB |
Output is correct |
25 |
Correct |
175 ms |
29940 KB |
Output is correct |
26 |
Correct |
178 ms |
29964 KB |
Output is correct |
27 |
Correct |
226 ms |
30108 KB |
Output is correct |
28 |
Correct |
259 ms |
30108 KB |
Output is correct |
29 |
Correct |
222 ms |
30108 KB |
Output is correct |
30 |
Correct |
322 ms |
30140 KB |
Output is correct |
31 |
Correct |
271 ms |
30140 KB |
Output is correct |
32 |
Correct |
522 ms |
49832 KB |
Output is correct |
33 |
Correct |
489 ms |
54072 KB |
Output is correct |
34 |
Correct |
495 ms |
58308 KB |
Output is correct |
35 |
Correct |
495 ms |
62560 KB |
Output is correct |
36 |
Runtime error |
489 ms |
66560 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
37 |
Halted |
0 ms |
0 KB |
- |