답안 #58771

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
58771 2018-07-19T09:45:42 Z andy627 트리 (KOI16_tree) C++17
100 / 100
528 ms 26260 KB
#include <stdio.h>
#include <vector>
#include <algorithm>
#define pii pair<int,int>
#define ff first
#define ss second
#define ele (1<<18)
#define INF 2e9
using namespace std;

struct node{
    int root=INF;
}sgt[ele<<1];

vector<int> edge[222222];

int num_pos=1;
pii sub[222222];
bool is_gone[222222];

void dfs_num(int v){
    sub[v].ff=num_pos;
    for(int i=0;i<edge[v].size();i++){
        if(is_gone[edge[v][i]]) continue;
        is_gone[edge[v][i]]=1;
        dfs_num(edge[v][i]);
    }
    sub[v].ss=num_pos++;
}

void spread_(int w,int l,int r){
    if(w>=ele) return;
    sgt[w<<1].root=min(sgt[w<<1].root,sgt[w].root);
    sgt[w<<1|1].root=min(sgt[w<<1|1].root,sgt[w].root);
}

void insert_(int w,int l,int r,int s,int e,int g){
    if(s>e) return;
    if(sgt[w].root<g) return;

    spread_(w,l,r);
    if(s==l && e==r){
        sgt[w].root=g;
        return;
    }

    int m=(l+r)>>1;
    if(s<=m) insert_(w<<1,l,m,s,min(m,e),g);
    if(m<e) insert_(w<<1|1,m+1,r,max(s,m+1),e,g);
}

int search_(int w,int l,int r,int p){
    if(l==p && r==p) return sgt[w].root;

    spread_(w,l,r);

    int m=(l+r)>>1;
    if(p<=m) return search_(w<<1,l,m,p);
    return search_(w<<1|1,m+1,r,p);
}

int main(){
    int n,q;

    scanf("%d %d",&n,&q);
    for(int i=2;i<=n;i++){
        int par;
        scanf("%d",&par);
        edge[par].push_back(i);
        edge[i].push_back(par);
    }

    is_gone[1]=1; dfs_num(1);
    insert_(1,1,ele,1,n,n);

    while(q--){
        int a,b,c;
        scanf("%d %d %d",&a,&b,&c);

        int root_a=search_(1,1,ele,sub[a].ss);
        int root_b=search_(1,1,ele,sub[b].ss);

        if(c){
            if(root_a==root_b){
                insert_(1,1,ele,sub[a].ff,sub[a].ss,sub[a].ss);
                printf("YES\n");
            }
            else{
                insert_(1,1,ele,sub[b].ff,sub[b].ss,sub[b].ss);
                printf("NO\n");
            }
        }
        else{
            if(root_a==root_b) printf("YES\n");
            else printf("NO\n");
        }
    }

    return 0;
}

Compilation message

tree.cpp: In function 'void dfs_num(int)':
tree.cpp:23:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<edge[v].size();i++){
                 ~^~~~~~~~~~~~~~~
tree.cpp: In function 'int main()':
tree.cpp:65: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:68:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d",&par);
         ~~~~~^~~~~~~~~~~
tree.cpp:78: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 5624 KB Output is correct
2 Correct 10 ms 5864 KB Output is correct
3 Correct 9 ms 5864 KB Output is correct
4 Correct 9 ms 5864 KB Output is correct
5 Correct 9 ms 5864 KB Output is correct
6 Correct 8 ms 5864 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 10 ms 5864 KB Output is correct
9 Correct 11 ms 5992 KB Output is correct
10 Correct 8 ms 6064 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 5624 KB Output is correct
2 Correct 10 ms 5864 KB Output is correct
3 Correct 9 ms 5864 KB Output is correct
4 Correct 9 ms 5864 KB Output is correct
5 Correct 9 ms 5864 KB Output is correct
6 Correct 8 ms 5864 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 10 ms 5864 KB Output is correct
9 Correct 11 ms 5992 KB Output is correct
10 Correct 8 ms 6064 KB Output is correct
11 Correct 10 ms 6064 KB Output is correct
12 Correct 12 ms 6064 KB Output is correct
13 Correct 8 ms 6064 KB Output is correct
14 Correct 9 ms 6064 KB Output is correct
15 Correct 9 ms 6064 KB Output is correct
16 Correct 11 ms 6104 KB Output is correct
17 Correct 10 ms 6104 KB Output is correct
18 Correct 9 ms 6104 KB Output is correct
19 Correct 11 ms 6120 KB Output is correct
20 Correct 11 ms 6164 KB Output is correct
21 Correct 10 ms 6172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 5624 KB Output is correct
2 Correct 10 ms 5864 KB Output is correct
3 Correct 9 ms 5864 KB Output is correct
4 Correct 9 ms 5864 KB Output is correct
5 Correct 9 ms 5864 KB Output is correct
6 Correct 8 ms 5864 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 10 ms 5864 KB Output is correct
9 Correct 11 ms 5992 KB Output is correct
10 Correct 8 ms 6064 KB Output is correct
11 Correct 10 ms 6064 KB Output is correct
12 Correct 12 ms 6064 KB Output is correct
13 Correct 8 ms 6064 KB Output is correct
14 Correct 9 ms 6064 KB Output is correct
15 Correct 9 ms 6064 KB Output is correct
16 Correct 11 ms 6104 KB Output is correct
17 Correct 10 ms 6104 KB Output is correct
18 Correct 9 ms 6104 KB Output is correct
19 Correct 11 ms 6120 KB Output is correct
20 Correct 11 ms 6164 KB Output is correct
21 Correct 10 ms 6172 KB Output is correct
22 Correct 223 ms 9052 KB Output is correct
23 Correct 198 ms 9564 KB Output is correct
24 Correct 185 ms 9564 KB Output is correct
25 Correct 223 ms 9640 KB Output is correct
26 Correct 220 ms 9640 KB Output is correct
27 Correct 192 ms 9656 KB Output is correct
28 Correct 236 ms 9656 KB Output is correct
29 Correct 181 ms 9656 KB Output is correct
30 Correct 147 ms 9784 KB Output is correct
31 Correct 226 ms 9784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 5624 KB Output is correct
2 Correct 10 ms 5864 KB Output is correct
3 Correct 9 ms 5864 KB Output is correct
4 Correct 9 ms 5864 KB Output is correct
5 Correct 9 ms 5864 KB Output is correct
6 Correct 8 ms 5864 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 10 ms 5864 KB Output is correct
9 Correct 11 ms 5992 KB Output is correct
10 Correct 8 ms 6064 KB Output is correct
11 Correct 424 ms 25340 KB Output is correct
12 Correct 482 ms 25372 KB Output is correct
13 Correct 471 ms 25372 KB Output is correct
14 Correct 469 ms 25400 KB Output is correct
15 Correct 491 ms 25424 KB Output is correct
16 Correct 423 ms 25916 KB Output is correct
17 Correct 498 ms 25956 KB Output is correct
18 Correct 508 ms 25956 KB Output is correct
19 Correct 486 ms 26160 KB Output is correct
20 Correct 444 ms 26260 KB Output is correct
21 Correct 510 ms 26260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 5624 KB Output is correct
2 Correct 10 ms 5864 KB Output is correct
3 Correct 9 ms 5864 KB Output is correct
4 Correct 9 ms 5864 KB Output is correct
5 Correct 9 ms 5864 KB Output is correct
6 Correct 8 ms 5864 KB Output is correct
7 Correct 8 ms 5864 KB Output is correct
8 Correct 10 ms 5864 KB Output is correct
9 Correct 11 ms 5992 KB Output is correct
10 Correct 8 ms 6064 KB Output is correct
11 Correct 10 ms 6064 KB Output is correct
12 Correct 12 ms 6064 KB Output is correct
13 Correct 8 ms 6064 KB Output is correct
14 Correct 9 ms 6064 KB Output is correct
15 Correct 9 ms 6064 KB Output is correct
16 Correct 11 ms 6104 KB Output is correct
17 Correct 10 ms 6104 KB Output is correct
18 Correct 9 ms 6104 KB Output is correct
19 Correct 11 ms 6120 KB Output is correct
20 Correct 11 ms 6164 KB Output is correct
21 Correct 10 ms 6172 KB Output is correct
22 Correct 223 ms 9052 KB Output is correct
23 Correct 198 ms 9564 KB Output is correct
24 Correct 185 ms 9564 KB Output is correct
25 Correct 223 ms 9640 KB Output is correct
26 Correct 220 ms 9640 KB Output is correct
27 Correct 192 ms 9656 KB Output is correct
28 Correct 236 ms 9656 KB Output is correct
29 Correct 181 ms 9656 KB Output is correct
30 Correct 147 ms 9784 KB Output is correct
31 Correct 226 ms 9784 KB Output is correct
32 Correct 424 ms 25340 KB Output is correct
33 Correct 482 ms 25372 KB Output is correct
34 Correct 471 ms 25372 KB Output is correct
35 Correct 469 ms 25400 KB Output is correct
36 Correct 491 ms 25424 KB Output is correct
37 Correct 423 ms 25916 KB Output is correct
38 Correct 498 ms 25956 KB Output is correct
39 Correct 508 ms 25956 KB Output is correct
40 Correct 486 ms 26160 KB Output is correct
41 Correct 444 ms 26260 KB Output is correct
42 Correct 510 ms 26260 KB Output is correct
43 Correct 450 ms 26260 KB Output is correct
44 Correct 384 ms 26260 KB Output is correct
45 Correct 396 ms 26260 KB Output is correct
46 Correct 332 ms 26260 KB Output is correct
47 Correct 464 ms 26260 KB Output is correct
48 Correct 448 ms 26260 KB Output is correct
49 Correct 280 ms 26260 KB Output is correct
50 Correct 276 ms 26260 KB Output is correct
51 Correct 528 ms 26260 KB Output is correct