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;
const int inf = 2e5 + 9;
int par[inf];
struct query {
int a, b, c;
query(int _a, int _b, int _c) : a(_a), b(_b), c(_c) {}
};
struct ds {
int par[inf];
int find(int u) {
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
void merge(int u, int v) {
u = find(u); v = find(v);
if (u == v) return;
par[u] = v;
}
} tree;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) tree.par[i] = i;
for (int i = 2; i <= n; i++) cin >> par[i];
vector<query> queries;
for (int i = 0; i < n - 1 + m; i++) {
int x; cin >> x;
if (x == 0) {
int a; cin >> a;
queries.emplace_back(0, a, 0);
} else {
int a, b; cin >> a >> b;
queries.emplace_back(1, a, b);
}
}
reverse(queries.begin(), queries.end());
vector<string> ans;
for (auto q : queries) {
if (q.a == 0) tree.merge(q.b, par[q.b]);
else {
int u = tree.find(q.b), v = tree.find(q.c);
if (u == v) ans.push_back("YES");
else ans.push_back("NO");
}
}
reverse(ans.begin(), ans.end());
for (auto aa : ans) {
cout << aa << '\n';
}
}
# | 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... |