Submission #30882

#TimeUsernameProblemLanguageResultExecution timeMemory
30882cgiosy트리 (KOI16_treeM)C++14
22 / 100
589 ms18056 KiB
#include <bits/stdc++.h>
using namespace std;

int a, b, c, d, x, n, q;
int *root, *parent;
vector<int> *tree;
vector<int> *t;

void f(int i)
{
	root[i] = b;

	for(int j : tree[i])
		f(j);
}

int main()
{
	cin >> n >> q;
	parent = new int[n + 2]();
	root = new int[n + 2]();
	tree = new vector<int>[n + 2];

	for(int i = 2; i <= n; i++)
	{
		cin >> a;
		tree[a].push_back(i);
		parent[i] = a;
	}

	for(int i = 1, m = n + q; i < m; i++)
	{
		cin >> x;

		if(x == 0)
		{
			cin >> b;
			t = &tree[parent[b]];
			t->erase(remove(t->begin(), t->end(), b), t->end());
			f(b);
		}
		else
		{
			cin >> c >> d;
			cout << (root[c] == root[d] ? "YES" : "NO") << '\n';
		}
	}

	cout << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...