Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstring>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <unordered_map>
- #include <numeric>
- #include <iomanip>
- using namespace std;
- #define pii pair<int , int>
- #define ll long long
- #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
- const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
- const long long dl[2] = {1, -1};
- const long long MOD = 1000000007;
- const long long MAXN = 200005;
- int N, Q, par[MAXN], p[MAXN], sz[MAXN];
- vector<pair<int, pii>> q;
- vector<string> ans;
- int Find(int x){
- if(x == p[x]) return x;
- return p[x] = Find(p[x]);
- }
- void Union(int a, int b){
- a = Find(a); b = Find(b);
- if(a == b) return;
- if(sz[a] > sz[b]) swap(a, b);
- p[a] = b;
- sz[b] += sz[a];
- }
- string Check(int a, int b){
- if(Find(a) == Find(b)){
- return "YES";
- }
- else{
- return "NO";
- }
- }
- void init(){
- for(int i = 1; i <= N; i++){
- p[i] = i;
- sz[i] = 1;
- }
- }
- int main() {
- FAST;
- cin >> N >> Q;
- for(int i = 2; i <= N; i++){
- cin >> par[i];
- }
- init();
- for(int x, y, z, i = 0; i < N + Q - 1; i++){
- cin >> x;
- if(x == 0){
- cin >> y;
- q.push_back({x, {y, -1}});
- }
- else{
- cin >> y >> z;
- q.push_back({x, {y, z}});
- }
- }
- reverse(q.begin(), q.end());
- for(auto x : q){
- if(x.first == 0){
- Union(x.second.first, par[x.second.first]);
- }
- else{
- ans.push_back(Check(x.second.first, x.second.second));
- }
- }
- reverse(ans.begin(), ans.end());
- for(auto x : ans){
- cout << x << "\n";
- }
- }
- /*
- 11 5
- 7
- 4
- 1
- 9
- 11
- 1
- 11
- 1
- 3
- 7
- 0 11
- 1 8 5
- 1 3 9
- 0 10
- 0 9
- 0 7
- 1 2 7
- 0 5
- 1 1 10
- 0 8
- 0 6
- 0 2
- 1 1 3
- 0 3
- 0 4
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement