# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
159745 | geon040702 | 트리 (KOI16_treeM) | C++14 | 181 ms | 14968 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 <bits/stdc++.h>
using namespace std;
int Find_Root(int Node);
int Tree[200010];
int Parent[200010];
int Input[400010][3];
int answer[400010];
int main(void)
{
int Node, Ques, ans_cnt, i;
scanf("%d %d", &Node, &Ques);
ans_cnt = Ques;
Tree[1] = Parent[1] = 1;
for(i=2;i<=Node;i++) {
Tree[i] = i;
scanf("%d", &Parent[i]);
}
for(i=1;i<=Node+Ques-1;i++) {
scanf("%d %d", &Input[i][0], &Input[i][1]);
if(Input[i][0] == 1) {
scanf("%d", &Input[i][2]);
}
}
for(i=Node+Ques-1;i>=1;i--) {
if(ans_cnt == 0) {
break;
}
if(Input[i][0] == 0) {
Tree[Input[i][1]] = Parent[Input[i][1]];
}
else {
if(Find_Root(Input[i][1]) == Find_Root(Input[i][2])) {
answer[ans_cnt] = 1;
}
ans_cnt--;
}
}
for(i=1;i<=Ques;i++) {
printf("%s\n", answer[i] ? "YES" : "NO");
}
return 0;
}
int Find_Root(int Node)
{
if(Tree[Node] != Node) {
Tree[Node] = Find_Root(Tree[Node]);
}
return Tree[Node];
}
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... |