# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
92575 |
2019-01-03T15:43:09 Z |
ansol4328 |
None (KOI16_treeM) |
C++11 |
|
466 ms |
48220 KB |
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
struct SegmentTree
{
vector<bool> tree;
int base;
void setup(int a)
{
base=1;
while(base<a) base=base<<1;
tree.resize(base*2+2,true);
base--;
}
void update(int idx)
{
idx+=base;
tree[idx]=false;
idx=idx>>1;
while(idx!=0)
{
tree[idx]=(tree[idx*2] && tree[idx*2+1]);
idx=idx>>1;
}
}
bool pos(int num, int st, int fn, int ns=1, int nf=-1)
{
if(nf==-1) nf=base+1;
if(fn<ns || nf<st) return true;
if(st<=ns && nf<=fn) return tree[num];
int mid=(ns+nf)>>1;
return (pos(num*2,st,fn,ns,mid) && pos(num*2+1,st,fn,mid+1,nf));
}
};
struct HeavyLightDecomposition
{
vector<vector<int> > lst;
vector<int> par, sub, depth;
vector<int> tail, dfsn, chain;
vector<SegmentTree> HLDSegTree;
int dn;
HeavyLightDecomposition(int a)
{
lst.resize(a+2);
sub.resize(a+2);
par.resize(a+2);
depth.resize(a+2);
dn=0;
tail.resize(a+2);
dfsn.resize(a+2);
chain.resize(a+2);
HLDSegTree.resize(a+2);
}
void add_edge(int v1, int v2)
{
lst[v1].push_back(v2);
}
void get_subsize(int cur, int prev);
void HLD(int cur, int prev, int c_num);
void construct_segtree(int a);
void update_segtree(int v1, int v2);
bool linked(int x, int y);
};
void HeavyLightDecomposition::get_subsize(int cur, int prev)
{
sub[cur]=1;
for(int i=0 ; i<lst[cur].size() ; i++)
{
int nxt=lst[cur][i];
if(nxt!=prev)
{
HeavyLightDecomposition::get_subsize(nxt,cur);
sub[cur]+=sub[nxt];
}
}
}
void HeavyLightDecomposition::HLD(int cur, int prev, int c_num)
{
dfsn[cur]=++dn;
chain[dn]=c_num;
tail[c_num]=dn;
depth[dfsn[cur]]=depth[dfsn[prev]]+1;
par[dfsn[cur]]=dfsn[prev];
int idx=-1;
for(int i=0 ; i<lst[cur].size() ; i++)
{
int nxt=lst[cur][i];
if(nxt!=prev && (idx==-1 || sub[nxt]>sub[idx])) idx=nxt;
}
if(idx!=-1) HeavyLightDecomposition::HLD(idx,cur,c_num);
for(int i=0 ; i<lst[cur].size() ; i++)
{
int nxt=lst[cur][i];
if(nxt!=prev && nxt!=idx)
HeavyLightDecomposition::HLD(nxt,cur,dn+1);
}
}
void HeavyLightDecomposition::construct_segtree(int a)
{
for(int i=1 ; i<=a ; i++)
{
if(chain[dfsn[i]]==dfsn[i]) HLDSegTree[dfsn[i]].setup(tail[dfsn[i]]-dfsn[i]+1);
}
}
void HeavyLightDecomposition::update_segtree(int v1, int v2)
{
v1=dfsn[v1], v2=dfsn[v2];
if(depth[v1]>depth[v2]) swap(v1,v2);
if(v2==1) return;
int seg_num=chain[v2];
if(chain[v1]==chain[v2]) HLDSegTree[seg_num].update(v2-chain[v2]+1);
else HLDSegTree[seg_num].update(1);
}
bool HeavyLightDecomposition::linked(int x, int y)
{
bool ret;
x=dfsn[x];
y=dfsn[y];
if(x==y) return true;
while(chain[x]!=chain[y])
{
int x1=chain[x];
int y1=chain[y];
if(depth[x1]>depth[y1])
{
ret=HLDSegTree[chain[x]].pos(1,1,x-chain[x]+1);
x=par[x1];
}
else
{
ret=HLDSegTree[chain[y]].pos(1,1,y-chain[y]+1);
y=par[y1];
}
if(ret==false) return false;
}
if(depth[x]>depth[y]) swap(x,y);
if(x!=y) ret=HLDSegTree[chain[x]].pos(1,x-chain[x]+2,y-chain[x]+1);
return ret;
}
int main()
{
int n, m;
scanf("%d %d",&n,&m);
vector<int> par(n+2);
HeavyLightDecomposition T(n);
par[1]=1;
for(int i=2 ; i<=n ; i++)
{
int x;
scanf("%d",&x);
par[i]=x;
T.add_edge(x,i);
}
T.get_subsize(1,0);
T.HLD(1,0,1);
T.construct_segtree(n);
for(int i=0 ; i<m+n-1 ; i++)
{
int x, y, z;
scanf("%d",&x);
if(x==0)
{
scanf("%d",&y);
T.update_segtree(y,par[y]);
}
else
{
scanf("%d %d",&y,&z);
bool res=T.linked(y,z);
printf("%s\n",res ? "YES" : "NO");
}
}
return 0;
}
Compilation message
tree.cpp: In member function 'void HeavyLightDecomposition::get_subsize(int, int)':
tree.cpp:76:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0 ; i<lst[cur].size() ; i++)
~^~~~~~~~~~~~~~~~
tree.cpp: In member function 'void HeavyLightDecomposition::HLD(int, int, int)':
tree.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0 ; i<lst[cur].size() ; i++)
~^~~~~~~~~~~~~~~~
tree.cpp:101:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0 ; i<lst[cur].size() ; i++)
~^~~~~~~~~~~~~~~~
tree.cpp: In function 'int main()':
tree.cpp:157:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&n,&m);
~~~~~^~~~~~~~~~~~~~~
tree.cpp:164:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x);
~~~~~^~~~~~~~~
tree.cpp:174:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&x);
~~~~~^~~~~~~~~
tree.cpp:177:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&y);
~~~~~^~~~~~~~~
tree.cpp:182:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&y,&z);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
520 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
520 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
92 ms |
3612 KB |
Output is correct |
23 |
Correct |
94 ms |
3576 KB |
Output is correct |
24 |
Correct |
94 ms |
3576 KB |
Output is correct |
25 |
Correct |
95 ms |
3576 KB |
Output is correct |
26 |
Correct |
96 ms |
3704 KB |
Output is correct |
27 |
Correct |
105 ms |
3576 KB |
Output is correct |
28 |
Correct |
108 ms |
3672 KB |
Output is correct |
29 |
Correct |
105 ms |
3648 KB |
Output is correct |
30 |
Correct |
129 ms |
3792 KB |
Output is correct |
31 |
Correct |
115 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
427 ms |
48164 KB |
Output is correct |
2 |
Correct |
455 ms |
48132 KB |
Output is correct |
3 |
Correct |
466 ms |
48120 KB |
Output is correct |
4 |
Correct |
430 ms |
48120 KB |
Output is correct |
5 |
Correct |
461 ms |
48156 KB |
Output is correct |
6 |
Correct |
424 ms |
47992 KB |
Output is correct |
7 |
Correct |
423 ms |
48068 KB |
Output is correct |
8 |
Correct |
440 ms |
47992 KB |
Output is correct |
9 |
Correct |
429 ms |
48212 KB |
Output is correct |
10 |
Correct |
362 ms |
48220 KB |
Output is correct |
11 |
Correct |
373 ms |
48056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
504 KB |
Output is correct |
3 |
Correct |
3 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
520 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
376 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
3 ms |
504 KB |
Output is correct |
10 |
Correct |
3 ms |
504 KB |
Output is correct |
11 |
Correct |
3 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
3 ms |
504 KB |
Output is correct |
14 |
Correct |
3 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
3 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
504 KB |
Output is correct |
21 |
Correct |
3 ms |
504 KB |
Output is correct |
22 |
Correct |
92 ms |
3612 KB |
Output is correct |
23 |
Correct |
94 ms |
3576 KB |
Output is correct |
24 |
Correct |
94 ms |
3576 KB |
Output is correct |
25 |
Correct |
95 ms |
3576 KB |
Output is correct |
26 |
Correct |
96 ms |
3704 KB |
Output is correct |
27 |
Correct |
105 ms |
3576 KB |
Output is correct |
28 |
Correct |
108 ms |
3672 KB |
Output is correct |
29 |
Correct |
105 ms |
3648 KB |
Output is correct |
30 |
Correct |
129 ms |
3792 KB |
Output is correct |
31 |
Correct |
115 ms |
3676 KB |
Output is correct |
32 |
Correct |
427 ms |
48164 KB |
Output is correct |
33 |
Correct |
455 ms |
48132 KB |
Output is correct |
34 |
Correct |
466 ms |
48120 KB |
Output is correct |
35 |
Correct |
430 ms |
48120 KB |
Output is correct |
36 |
Correct |
461 ms |
48156 KB |
Output is correct |
37 |
Correct |
424 ms |
47992 KB |
Output is correct |
38 |
Correct |
423 ms |
48068 KB |
Output is correct |
39 |
Correct |
440 ms |
47992 KB |
Output is correct |
40 |
Correct |
429 ms |
48212 KB |
Output is correct |
41 |
Correct |
362 ms |
48220 KB |
Output is correct |
42 |
Correct |
373 ms |
48056 KB |
Output is correct |
43 |
Correct |
333 ms |
33400 KB |
Output is correct |
44 |
Correct |
434 ms |
33588 KB |
Output is correct |
45 |
Correct |
430 ms |
33784 KB |
Output is correct |
46 |
Correct |
368 ms |
33508 KB |
Output is correct |
47 |
Correct |
387 ms |
33628 KB |
Output is correct |
48 |
Correct |
259 ms |
33560 KB |
Output is correct |
49 |
Correct |
280 ms |
33784 KB |
Output is correct |
50 |
Correct |
286 ms |
33912 KB |
Output is correct |
51 |
Correct |
222 ms |
36856 KB |
Output is correct |