Submission #57817

# Submission time Handle Problem Language Result Execution time Memory
57817 2018-07-16T09:22:32 Z red1108 None (KOI16_tree) C++17
0 / 100
14 ms 9848 KB
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int n,q;
set<int> tree[200010];
int seg[800010],par[800010],cnt=0,si=1,color[800010],ccnt=1,bumo[200010];
pair<int,int> range[200010];
bool cut[200010];
void dfs(int x)
{
    range[x].first=++cnt;
    for(auto i:tree[x]) dfs(i);
    range[x].second=cnt;
}
void rename(int x,int newc)
{
    color[x]=newc;
    for(auto i:tree[x]) rename(i,newc);
}
void gang(int x, int k)
{
    x=x+si-1;
    int delta=k-seg[x];
    while(x)
    {
        seg[x]+=delta;
        x/=2;
    }
}
int getsize(int x, int l, int r, int s, int e)
{
    if(l>r||e<l||r<s)return 0;
    if(s<=l&&r<=e)return seg[x];
    return getsize(x*2,l,(l+r)/2,s,e)+getsize(x*2+1,(l+r)/2+1,r,s,e);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie();
    int i,a,b,c,flag;
    scanf("%d %d", &n, &q);
    while(si<n)si*=2;
    for(i=2; i<=n; i++)
    {
        cin>>a;
        bumo[i]=a;
        tree[a].insert(i);
    }
    for(i=1;i<=n;i++) color[i]=1;
    par[1]=1;
    dfs(1);
    for(i=1;i<=n;i++) gang(i,1);
    for(i=1;i<=q;i++,flag=0)
    {
        cin>>a>>b>>c;
        if(color[a]==color[b])
        {
            cout<<"YES"<<"\n";
            flag=1;
        }
        else cout<<"NO"<<"\n";
        if(c==0) continue;
        if(!flag) a=b;
        if(cut[a])continue;
        cut[a]=1;
        tree[bumo[a]].erase(a);
        int sip, sia;
        sia=getsize(1,1,si,range[a].first+1,range[a].second)+1;
        gang(range[a].first,(-1)*sia+1);
        sip=getsize(1,1,si,range[par[color[a]]].first+1,range[par[color[a]]].second)+1;
        if(sip<=sia)
        {
            par[ccnt+1]=par[color[a]];
            int tmp=color[a];
            rename(par[color[a]],++ccnt);
            par[tmp]=a;
        }
        else
        {
            rename(a,++ccnt);
            par[ccnt]=a;
        }
    }
}

Compilation message

tree.cpp: In function 'int main()':
tree.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &q);
     ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 9848 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 9848 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 9848 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 9848 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 9848 KB Output isn't correct
2 Halted 0 ms 0 KB -