답안 #218081

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
218081 2020-04-01T06:23:43 Z brainwarego 트리 (KOI16_tree) C++14
100 / 100
307 ms 16376 KB
#include <stdio.h>
#define LM 200010
 
int N, Q, par[LM], group[LM], gn;
int used[LM], gtop[LM] = {1};
struct Node{
    int node;
    Node*next;
}adj[LM], buf[LM*3];
int bCnt;
 
struct Queue{
    Node*now;
    Node* hasNext(){
        while(now->next && used[now->next->node]){
            now->next = now->next->next;
        }
        now = now->next;
        return now;
    }
}xQue[LM], yQue[LM];
int xf, xe, yf, ye;
 
void push_adj(int s, int e){
    buf[bCnt] = {e, adj[s].next};
    adj[s].next = buf + bCnt++;
}
 
void reGroup(int k){
    group[k] = gn;
    for(Node*p = adj+k;p->next;){
        if(used[p->next->node]){
            p->next = p->next->next;
        }
        else{
            reGroup(p->next->node);
            p = p->next;
        }
    }
}
 
void split(int a, int b){
    if(a==0) return;
    par[b] = 0;
    used[b] = 1;
    a = gtop[group[a]];
 
    int flag = 0;
    xf = xe = yf = ye = 0;
    xQue[xe++] = {adj+a};
    yQue[ye++] = {adj+b};
    while(1){
        while(xf<xe && !xQue[xf].hasNext())
            ++xf;
        if(xf >= xe)
            break;
        xQue[xe++] = {adj + xQue[xf].now->node};
 
        while(yf<ye && !yQue[yf].hasNext())
            ++yf;
        if(yf >= ye){
            flag = 1;
            break;
        }
        yQue[ye++] = {adj + yQue[yf].now->node};
    }
    gn++;
    if(flag){
        gtop[gn] = b;
        reGroup(b);
    }
    else{
        gtop[group[a]] = b;
        gtop[gn] = a;
        reGroup(a);
    }
}
 
int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%d %d", &N, &Q);
    int i, b, c, d;
    for(i=2;i<=N;++i){
        scanf("%d", &b);
        par[i] = b;
        push_adj(b, i);
    }
 
    for(i=0;i<Q;++i){
        scanf("%d %d %d", &b, &c, &d);
        int flag = group[b]==group[c];
        puts(flag? "YES":"NO");
        if(d){
            if(flag) split(par[b], b);
            else split(par[c], c);
        }
    }
    return 0;
}

Compilation message

tree.cpp: In function 'int main()':
tree.cpp:81: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:84:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &b);
         ~~~~~^~~~~~~~~~
tree.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &b, &c, &d);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 74 ms 3320 KB Output is correct
23 Correct 74 ms 3288 KB Output is correct
24 Correct 74 ms 3320 KB Output is correct
25 Correct 75 ms 3320 KB Output is correct
26 Correct 74 ms 3320 KB Output is correct
27 Correct 74 ms 3448 KB Output is correct
28 Correct 73 ms 3468 KB Output is correct
29 Correct 73 ms 3448 KB Output is correct
30 Correct 74 ms 3448 KB Output is correct
31 Correct 75 ms 3448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 292 ms 15096 KB Output is correct
12 Correct 305 ms 16240 KB Output is correct
13 Correct 281 ms 15096 KB Output is correct
14 Correct 307 ms 15224 KB Output is correct
15 Correct 289 ms 16376 KB Output is correct
16 Correct 289 ms 15864 KB Output is correct
17 Correct 163 ms 15096 KB Output is correct
18 Correct 165 ms 15096 KB Output is correct
19 Correct 172 ms 15124 KB Output is correct
20 Correct 161 ms 15096 KB Output is correct
21 Correct 267 ms 14716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 6 ms 384 KB Output is correct
22 Correct 74 ms 3320 KB Output is correct
23 Correct 74 ms 3288 KB Output is correct
24 Correct 74 ms 3320 KB Output is correct
25 Correct 75 ms 3320 KB Output is correct
26 Correct 74 ms 3320 KB Output is correct
27 Correct 74 ms 3448 KB Output is correct
28 Correct 73 ms 3468 KB Output is correct
29 Correct 73 ms 3448 KB Output is correct
30 Correct 74 ms 3448 KB Output is correct
31 Correct 75 ms 3448 KB Output is correct
32 Correct 292 ms 15096 KB Output is correct
33 Correct 305 ms 16240 KB Output is correct
34 Correct 281 ms 15096 KB Output is correct
35 Correct 307 ms 15224 KB Output is correct
36 Correct 289 ms 16376 KB Output is correct
37 Correct 289 ms 15864 KB Output is correct
38 Correct 163 ms 15096 KB Output is correct
39 Correct 165 ms 15096 KB Output is correct
40 Correct 172 ms 15124 KB Output is correct
41 Correct 161 ms 15096 KB Output is correct
42 Correct 267 ms 14716 KB Output is correct
43 Correct 148 ms 13132 KB Output is correct
44 Correct 140 ms 13308 KB Output is correct
45 Correct 143 ms 13176 KB Output is correct
46 Correct 149 ms 13432 KB Output is correct
47 Correct 140 ms 13272 KB Output is correct
48 Correct 115 ms 9208 KB Output is correct
49 Correct 113 ms 9336 KB Output is correct
50 Correct 118 ms 10376 KB Output is correct
51 Correct 209 ms 16248 KB Output is correct