답안 #57821

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57821 2018-07-16T09:30:44 Z red1108 트리 (KOI16_tree) C++17
55 / 100
2000 ms 32092 KB
#include <stdio.h>
#include <vector>
#include <set>
using namespace std;
int n,q;
set<int> tree[200010];
int seg[800010],par[200010],cnt=0,si=1,color[200010],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()
{
    int i,a,b,c,flag;
    scanf("%d %d", &n, &q);
    while(si<n)si*=2;
    for(i=2; i<=n; i++)
    {
        scanf("%d", &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)
    {
        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) a=b;
        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:40: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:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a);
         ~~~~~^~~~~~~~~~
tree.cpp:54: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);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9956 KB Output is correct
3 Correct 12 ms 10164 KB Output is correct
4 Correct 13 ms 10164 KB Output is correct
5 Correct 13 ms 10164 KB Output is correct
6 Correct 14 ms 10164 KB Output is correct
7 Correct 12 ms 10164 KB Output is correct
8 Correct 11 ms 10164 KB Output is correct
9 Correct 13 ms 10164 KB Output is correct
10 Correct 14 ms 10164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9956 KB Output is correct
3 Correct 12 ms 10164 KB Output is correct
4 Correct 13 ms 10164 KB Output is correct
5 Correct 13 ms 10164 KB Output is correct
6 Correct 14 ms 10164 KB Output is correct
7 Correct 12 ms 10164 KB Output is correct
8 Correct 11 ms 10164 KB Output is correct
9 Correct 13 ms 10164 KB Output is correct
10 Correct 14 ms 10164 KB Output is correct
11 Correct 12 ms 10196 KB Output is correct
12 Correct 12 ms 10196 KB Output is correct
13 Correct 12 ms 10196 KB Output is correct
14 Correct 11 ms 10196 KB Output is correct
15 Correct 12 ms 10196 KB Output is correct
16 Correct 13 ms 10196 KB Output is correct
17 Correct 12 ms 10196 KB Output is correct
18 Correct 12 ms 10196 KB Output is correct
19 Correct 11 ms 10196 KB Output is correct
20 Correct 11 ms 10196 KB Output is correct
21 Correct 13 ms 10196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9956 KB Output is correct
3 Correct 12 ms 10164 KB Output is correct
4 Correct 13 ms 10164 KB Output is correct
5 Correct 13 ms 10164 KB Output is correct
6 Correct 14 ms 10164 KB Output is correct
7 Correct 12 ms 10164 KB Output is correct
8 Correct 11 ms 10164 KB Output is correct
9 Correct 13 ms 10164 KB Output is correct
10 Correct 14 ms 10164 KB Output is correct
11 Correct 12 ms 10196 KB Output is correct
12 Correct 12 ms 10196 KB Output is correct
13 Correct 12 ms 10196 KB Output is correct
14 Correct 11 ms 10196 KB Output is correct
15 Correct 12 ms 10196 KB Output is correct
16 Correct 13 ms 10196 KB Output is correct
17 Correct 12 ms 10196 KB Output is correct
18 Correct 12 ms 10196 KB Output is correct
19 Correct 11 ms 10196 KB Output is correct
20 Correct 11 ms 10196 KB Output is correct
21 Correct 13 ms 10196 KB Output is correct
22 Correct 147 ms 11276 KB Output is correct
23 Correct 143 ms 11280 KB Output is correct
24 Correct 136 ms 11280 KB Output is correct
25 Correct 139 ms 11280 KB Output is correct
26 Correct 146 ms 11280 KB Output is correct
27 Correct 181 ms 11280 KB Output is correct
28 Correct 119 ms 11280 KB Output is correct
29 Correct 100 ms 11280 KB Output is correct
30 Correct 96 ms 11280 KB Output is correct
31 Correct 102 ms 11280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9956 KB Output is correct
3 Correct 12 ms 10164 KB Output is correct
4 Correct 13 ms 10164 KB Output is correct
5 Correct 13 ms 10164 KB Output is correct
6 Correct 14 ms 10164 KB Output is correct
7 Correct 12 ms 10164 KB Output is correct
8 Correct 11 ms 10164 KB Output is correct
9 Correct 13 ms 10164 KB Output is correct
10 Correct 14 ms 10164 KB Output is correct
11 Correct 1238 ms 31436 KB Output is correct
12 Correct 1125 ms 31592 KB Output is correct
13 Correct 1202 ms 31592 KB Output is correct
14 Correct 1249 ms 31592 KB Output is correct
15 Correct 1111 ms 31620 KB Output is correct
16 Correct 1122 ms 31620 KB Output is correct
17 Correct 615 ms 32052 KB Output is correct
18 Correct 678 ms 32052 KB Output is correct
19 Correct 565 ms 32052 KB Output is correct
20 Correct 524 ms 32092 KB Output is correct
21 Correct 1032 ms 32092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9848 KB Output is correct
2 Correct 10 ms 9956 KB Output is correct
3 Correct 12 ms 10164 KB Output is correct
4 Correct 13 ms 10164 KB Output is correct
5 Correct 13 ms 10164 KB Output is correct
6 Correct 14 ms 10164 KB Output is correct
7 Correct 12 ms 10164 KB Output is correct
8 Correct 11 ms 10164 KB Output is correct
9 Correct 13 ms 10164 KB Output is correct
10 Correct 14 ms 10164 KB Output is correct
11 Correct 12 ms 10196 KB Output is correct
12 Correct 12 ms 10196 KB Output is correct
13 Correct 12 ms 10196 KB Output is correct
14 Correct 11 ms 10196 KB Output is correct
15 Correct 12 ms 10196 KB Output is correct
16 Correct 13 ms 10196 KB Output is correct
17 Correct 12 ms 10196 KB Output is correct
18 Correct 12 ms 10196 KB Output is correct
19 Correct 11 ms 10196 KB Output is correct
20 Correct 11 ms 10196 KB Output is correct
21 Correct 13 ms 10196 KB Output is correct
22 Correct 147 ms 11276 KB Output is correct
23 Correct 143 ms 11280 KB Output is correct
24 Correct 136 ms 11280 KB Output is correct
25 Correct 139 ms 11280 KB Output is correct
26 Correct 146 ms 11280 KB Output is correct
27 Correct 181 ms 11280 KB Output is correct
28 Correct 119 ms 11280 KB Output is correct
29 Correct 100 ms 11280 KB Output is correct
30 Correct 96 ms 11280 KB Output is correct
31 Correct 102 ms 11280 KB Output is correct
32 Correct 1238 ms 31436 KB Output is correct
33 Correct 1125 ms 31592 KB Output is correct
34 Correct 1202 ms 31592 KB Output is correct
35 Correct 1249 ms 31592 KB Output is correct
36 Correct 1111 ms 31620 KB Output is correct
37 Correct 1122 ms 31620 KB Output is correct
38 Correct 615 ms 32052 KB Output is correct
39 Correct 678 ms 32052 KB Output is correct
40 Correct 565 ms 32052 KB Output is correct
41 Correct 524 ms 32092 KB Output is correct
42 Correct 1032 ms 32092 KB Output is correct
43 Correct 685 ms 32092 KB Output is correct
44 Correct 705 ms 32092 KB Output is correct
45 Correct 644 ms 32092 KB Output is correct
46 Execution timed out 2050 ms 32092 KB Time limit exceeded
47 Halted 0 ms 0 KB -