Submission #92572

# Submission time Handle Problem Language Result Execution time Memory
92572 2019-01-03T15:28:57 Z ansol4328 None (KOI16_tree) C++
100 / 100
374 ms 47352 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 ; i++)
    {
        int x, y, z;
        scanf("%d %d %d",&x,&y,&z);
        bool res=T.linked(x,y);
        printf("%s\n",res ? "YES" : "NO");
        if(z==0) continue;
        if(res) T.update_segtree(x,par[x]);
        else T.update_segtree(y,par[y]);
    }
    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 %d %d",&x,&y,&z);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 500 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 500 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 72 ms 3680 KB Output is correct
23 Correct 70 ms 3596 KB Output is correct
24 Correct 70 ms 3576 KB Output is correct
25 Correct 71 ms 3576 KB Output is correct
26 Correct 72 ms 3572 KB Output is correct
27 Correct 84 ms 3576 KB Output is correct
28 Correct 85 ms 3652 KB Output is correct
29 Correct 83 ms 3576 KB Output is correct
30 Correct 107 ms 3760 KB Output is correct
31 Correct 96 ms 3704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 500 KB Output is correct
11 Correct 301 ms 46732 KB Output is correct
12 Correct 285 ms 46740 KB Output is correct
13 Correct 348 ms 46800 KB Output is correct
14 Correct 348 ms 46712 KB Output is correct
15 Correct 347 ms 46800 KB Output is correct
16 Correct 373 ms 46712 KB Output is correct
17 Correct 313 ms 47352 KB Output is correct
18 Correct 374 ms 47196 KB Output is correct
19 Correct 361 ms 47352 KB Output is correct
20 Correct 302 ms 47224 KB Output is correct
21 Correct 347 ms 46712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 504 KB Output is correct
3 Correct 2 ms 504 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 492 KB Output is correct
9 Correct 2 ms 504 KB Output is correct
10 Correct 2 ms 500 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 504 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 72 ms 3680 KB Output is correct
23 Correct 70 ms 3596 KB Output is correct
24 Correct 70 ms 3576 KB Output is correct
25 Correct 71 ms 3576 KB Output is correct
26 Correct 72 ms 3572 KB Output is correct
27 Correct 84 ms 3576 KB Output is correct
28 Correct 85 ms 3652 KB Output is correct
29 Correct 83 ms 3576 KB Output is correct
30 Correct 107 ms 3760 KB Output is correct
31 Correct 96 ms 3704 KB Output is correct
32 Correct 301 ms 46732 KB Output is correct
33 Correct 285 ms 46740 KB Output is correct
34 Correct 348 ms 46800 KB Output is correct
35 Correct 348 ms 46712 KB Output is correct
36 Correct 347 ms 46800 KB Output is correct
37 Correct 373 ms 46712 KB Output is correct
38 Correct 313 ms 47352 KB Output is correct
39 Correct 374 ms 47196 KB Output is correct
40 Correct 361 ms 47352 KB Output is correct
41 Correct 302 ms 47224 KB Output is correct
42 Correct 347 ms 46712 KB Output is correct
43 Correct 322 ms 31444 KB Output is correct
44 Correct 293 ms 31244 KB Output is correct
45 Correct 243 ms 31224 KB Output is correct
46 Correct 242 ms 31224 KB Output is correct
47 Correct 267 ms 31224 KB Output is correct
48 Correct 150 ms 31584 KB Output is correct
49 Correct 174 ms 31872 KB Output is correct
50 Correct 170 ms 31760 KB Output is correct
51 Correct 290 ms 35108 KB Output is correct