Submission #57855

# Submission time Handle Problem Language Result Execution time Memory
57855 2018-07-16T10:41:00 Z red1108 None (KOI16_tree) C++17
55 / 100
2000 ms 26632 KB
#include <stdio.h>
#include <vector>
#include <set>
using namespace std;
int n,q;
vector<int> tree[200010];
long long seg[800010];
int par[200010],cnt=0,si=1,color[200010],ccnt=1,bumo[200010],cut[200010];
pair<int,int> range[200010];
int checkcnt=0;
int s,e;
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)
{
    checkcnt++;
    color[x]=newc;
    for(auto i:tree[x]) if(!cut[i]) 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;
    }
}
long long getsize(int x, int l, int r)
{
    if(l>r||e<l||r<s||l<0||r<0)return 0;
    if(s<=l&&r<=e)return seg[x];
    return getsize(x*2,l,(l+r)/2)+getsize(x*2+1,(l+r)/2+1,r);
}
int main()
{
    /*
    freopen("input35", "r",stdin);
    freopen("output.txt", "w", stdout);
    */
    int i,a,b,c,flag,tmp,sip,sia;
    scanf("%d %d", &n,&q);
    while(si<n)si*=2;
    for(i=2; i<=n; i++)
    {
        scanf("%d", &a);
        bumo[i]=a;
        tree[a].emplace_back(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)
    {
        scanf("%d %d %d", &a, &b, &c);
        if(color[a]==color[b])
        {
            printf("YES\n");
            flag=1;
        }
        else printf("NO\n");
        if(c==0) continue;
        if(flag==0) a=b;
        cut[a]=1;
        s=range[a].first+1;
        e=range[a].second;
        sia=getsize(1,1,si)+1;
        gang(range[a].first,(-1)*sia+1);
        s=range[par[color[a]]].first+1;
        e=range[par[color[a]]].second;
        sip=getsize(1,1,si)+1;
        if(sip<sia)
        {
            par[ccnt+1]=par[color[a]];
            tmp=color[a];
            ccnt++;
            rename(par[color[a]],ccnt);
            par[tmp]=a;
        }
        else
        {
            ccnt++;
            rename(a,ccnt);
            par[ccnt]=a;
        }
    }
}

Compilation message

tree.cpp: In function 'int main()':
tree.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n,&q);
     ~~~~~^~~~~~~~~~~~~~~~
tree.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a);
         ~~~~~^~~~~~~~~~
tree.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &a, &b, &c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5220 KB Output is correct
3 Correct 7 ms 5220 KB Output is correct
4 Correct 7 ms 5224 KB Output is correct
5 Correct 9 ms 5224 KB Output is correct
6 Correct 10 ms 5268 KB Output is correct
7 Correct 8 ms 5268 KB Output is correct
8 Correct 9 ms 5268 KB Output is correct
9 Correct 9 ms 5308 KB Output is correct
10 Correct 10 ms 5308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5220 KB Output is correct
3 Correct 7 ms 5220 KB Output is correct
4 Correct 7 ms 5224 KB Output is correct
5 Correct 9 ms 5224 KB Output is correct
6 Correct 10 ms 5268 KB Output is correct
7 Correct 8 ms 5268 KB Output is correct
8 Correct 9 ms 5268 KB Output is correct
9 Correct 9 ms 5308 KB Output is correct
10 Correct 10 ms 5308 KB Output is correct
11 Correct 7 ms 5308 KB Output is correct
12 Correct 7 ms 5328 KB Output is correct
13 Correct 7 ms 5356 KB Output is correct
14 Correct 7 ms 5356 KB Output is correct
15 Correct 8 ms 5356 KB Output is correct
16 Correct 7 ms 5372 KB Output is correct
17 Correct 9 ms 5372 KB Output is correct
18 Correct 7 ms 5372 KB Output is correct
19 Correct 7 ms 5372 KB Output is correct
20 Correct 8 ms 5372 KB Output is correct
21 Correct 7 ms 5372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5220 KB Output is correct
3 Correct 7 ms 5220 KB Output is correct
4 Correct 7 ms 5224 KB Output is correct
5 Correct 9 ms 5224 KB Output is correct
6 Correct 10 ms 5268 KB Output is correct
7 Correct 8 ms 5268 KB Output is correct
8 Correct 9 ms 5268 KB Output is correct
9 Correct 9 ms 5308 KB Output is correct
10 Correct 10 ms 5308 KB Output is correct
11 Correct 7 ms 5308 KB Output is correct
12 Correct 7 ms 5328 KB Output is correct
13 Correct 7 ms 5356 KB Output is correct
14 Correct 7 ms 5356 KB Output is correct
15 Correct 8 ms 5356 KB Output is correct
16 Correct 7 ms 5372 KB Output is correct
17 Correct 9 ms 5372 KB Output is correct
18 Correct 7 ms 5372 KB Output is correct
19 Correct 7 ms 5372 KB Output is correct
20 Correct 8 ms 5372 KB Output is correct
21 Correct 7 ms 5372 KB Output is correct
22 Correct 128 ms 6396 KB Output is correct
23 Correct 201 ms 6412 KB Output is correct
24 Correct 155 ms 6428 KB Output is correct
25 Correct 137 ms 6500 KB Output is correct
26 Correct 196 ms 6540 KB Output is correct
27 Correct 137 ms 6540 KB Output is correct
28 Correct 103 ms 6540 KB Output is correct
29 Correct 106 ms 6540 KB Output is correct
30 Correct 110 ms 6540 KB Output is correct
31 Correct 103 ms 6588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5220 KB Output is correct
3 Correct 7 ms 5220 KB Output is correct
4 Correct 7 ms 5224 KB Output is correct
5 Correct 9 ms 5224 KB Output is correct
6 Correct 10 ms 5268 KB Output is correct
7 Correct 8 ms 5268 KB Output is correct
8 Correct 9 ms 5268 KB Output is correct
9 Correct 9 ms 5308 KB Output is correct
10 Correct 10 ms 5308 KB Output is correct
11 Correct 1159 ms 25548 KB Output is correct
12 Correct 1098 ms 25548 KB Output is correct
13 Correct 978 ms 25548 KB Output is correct
14 Correct 1183 ms 25548 KB Output is correct
15 Correct 1024 ms 25548 KB Output is correct
16 Correct 1100 ms 25548 KB Output is correct
17 Correct 700 ms 26428 KB Output is correct
18 Correct 563 ms 26560 KB Output is correct
19 Correct 631 ms 26560 KB Output is correct
20 Correct 537 ms 26632 KB Output is correct
21 Correct 1079 ms 26632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 7 ms 5220 KB Output is correct
3 Correct 7 ms 5220 KB Output is correct
4 Correct 7 ms 5224 KB Output is correct
5 Correct 9 ms 5224 KB Output is correct
6 Correct 10 ms 5268 KB Output is correct
7 Correct 8 ms 5268 KB Output is correct
8 Correct 9 ms 5268 KB Output is correct
9 Correct 9 ms 5308 KB Output is correct
10 Correct 10 ms 5308 KB Output is correct
11 Correct 7 ms 5308 KB Output is correct
12 Correct 7 ms 5328 KB Output is correct
13 Correct 7 ms 5356 KB Output is correct
14 Correct 7 ms 5356 KB Output is correct
15 Correct 8 ms 5356 KB Output is correct
16 Correct 7 ms 5372 KB Output is correct
17 Correct 9 ms 5372 KB Output is correct
18 Correct 7 ms 5372 KB Output is correct
19 Correct 7 ms 5372 KB Output is correct
20 Correct 8 ms 5372 KB Output is correct
21 Correct 7 ms 5372 KB Output is correct
22 Correct 128 ms 6396 KB Output is correct
23 Correct 201 ms 6412 KB Output is correct
24 Correct 155 ms 6428 KB Output is correct
25 Correct 137 ms 6500 KB Output is correct
26 Correct 196 ms 6540 KB Output is correct
27 Correct 137 ms 6540 KB Output is correct
28 Correct 103 ms 6540 KB Output is correct
29 Correct 106 ms 6540 KB Output is correct
30 Correct 110 ms 6540 KB Output is correct
31 Correct 103 ms 6588 KB Output is correct
32 Correct 1159 ms 25548 KB Output is correct
33 Correct 1098 ms 25548 KB Output is correct
34 Correct 978 ms 25548 KB Output is correct
35 Correct 1183 ms 25548 KB Output is correct
36 Correct 1024 ms 25548 KB Output is correct
37 Correct 1100 ms 25548 KB Output is correct
38 Correct 700 ms 26428 KB Output is correct
39 Correct 563 ms 26560 KB Output is correct
40 Correct 631 ms 26560 KB Output is correct
41 Correct 537 ms 26632 KB Output is correct
42 Correct 1079 ms 26632 KB Output is correct
43 Correct 335 ms 26632 KB Output is correct
44 Correct 462 ms 26632 KB Output is correct
45 Correct 377 ms 26632 KB Output is correct
46 Correct 1493 ms 26632 KB Output is correct
47 Correct 430 ms 26632 KB Output is correct
48 Correct 275 ms 26632 KB Output is correct
49 Correct 272 ms 26632 KB Output is correct
50 Correct 187 ms 26632 KB Output is correct
51 Execution timed out 2078 ms 26632 KB Time limit exceeded