# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
218081 | brainwarego | 트리 (KOI16_tree) | C++14 | 307 ms | 16376 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |