#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) ((x).begin()),((x).end())
#define usecppio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int V, q;
struct Disjoint_Set_Union
{
int parent[202020], size[202020];
Disjoint_Set_Union(int n = V)
{
init(n);
}
void init(int n)
{
for(int i=1;i<=n;i++)
{
parent[i]=i;
size[i]=1;
}
}
int Find(int k)
{
while(k!=parent[k])
{
parent[k]=parent[parent[k]];
k=parent[k];
}
return k;
}
int getSize(int k)
{
return size[Find(k)];
}
void unite(int x, int y)
{
int u=Find(x), v=Find(y);
if(u==v)
return;
if(size[u]>size[v])
swap(u, v);
size[v]+=size[u];
size[u] = 0;
parent[u] = parent[v];
}
};
Disjoint_Set_Union DSU;
int par[202020];
bool mark[202020];
vector <string> results;
vector <pair <int, int>> queries;
int main()
{
usecppio
cin >> V >> q;
DSU = Disjoint_Set_Union(V);
for (int i = 2; i<=V; i++)
cin >> par[i];
for (int i = 0; i<V+q-1; i++)
{
int a, b, c;
cin >> a;
if (a)
{
cin >> b >> c;
queries.push_back({b,c});
}
else
{
cin >> b;
queries.push_back({a,b});
mark[b] = true;
}
}
for (int i = V+q-2; i>=0; i--)
{
pair <int, int> p = queries[i];
if (!p.first)
DSU.unite(p.second,par[p.second]);
else
{
bool flag = true;
flag = (DSU.Find(p.first) == DSU.Find(p.second));
results.push_back((flag?"YES\n":"NO\n"));
}
}
reverse(all(results));
for (auto it:results)
cout << it;
}
Compilation message
tree.cpp: In function 'int main()':
tree.cpp:57:9: warning: '<anonymous>' may be used uninitialized in this function [-Wmaybe-uninitialized]
DSU = Disjoint_Set_Union(V);
~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2048 KB |
Output is correct |
2 |
Correct |
7 ms |
2048 KB |
Output is correct |
3 |
Correct |
7 ms |
2048 KB |
Output is correct |
4 |
Correct |
7 ms |
2048 KB |
Output is correct |
5 |
Correct |
7 ms |
2176 KB |
Output is correct |
6 |
Correct |
7 ms |
2048 KB |
Output is correct |
7 |
Correct |
7 ms |
2048 KB |
Output is correct |
8 |
Correct |
7 ms |
2048 KB |
Output is correct |
9 |
Correct |
7 ms |
2048 KB |
Output is correct |
10 |
Correct |
7 ms |
2048 KB |
Output is correct |
11 |
Correct |
7 ms |
2048 KB |
Output is correct |
12 |
Correct |
7 ms |
2048 KB |
Output is correct |
13 |
Correct |
7 ms |
2048 KB |
Output is correct |
14 |
Correct |
7 ms |
2048 KB |
Output is correct |
15 |
Correct |
7 ms |
2048 KB |
Output is correct |
16 |
Correct |
7 ms |
2048 KB |
Output is correct |
17 |
Correct |
7 ms |
2048 KB |
Output is correct |
18 |
Correct |
7 ms |
2048 KB |
Output is correct |
19 |
Correct |
7 ms |
2048 KB |
Output is correct |
20 |
Correct |
7 ms |
2048 KB |
Output is correct |
21 |
Correct |
7 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2048 KB |
Output is correct |
2 |
Correct |
7 ms |
2048 KB |
Output is correct |
3 |
Correct |
7 ms |
2048 KB |
Output is correct |
4 |
Correct |
7 ms |
2048 KB |
Output is correct |
5 |
Correct |
7 ms |
2176 KB |
Output is correct |
6 |
Correct |
7 ms |
2048 KB |
Output is correct |
7 |
Correct |
7 ms |
2048 KB |
Output is correct |
8 |
Correct |
7 ms |
2048 KB |
Output is correct |
9 |
Correct |
7 ms |
2048 KB |
Output is correct |
10 |
Correct |
7 ms |
2048 KB |
Output is correct |
11 |
Correct |
7 ms |
2048 KB |
Output is correct |
12 |
Correct |
7 ms |
2048 KB |
Output is correct |
13 |
Correct |
7 ms |
2048 KB |
Output is correct |
14 |
Correct |
7 ms |
2048 KB |
Output is correct |
15 |
Correct |
7 ms |
2048 KB |
Output is correct |
16 |
Correct |
7 ms |
2048 KB |
Output is correct |
17 |
Correct |
7 ms |
2048 KB |
Output is correct |
18 |
Correct |
7 ms |
2048 KB |
Output is correct |
19 |
Correct |
7 ms |
2048 KB |
Output is correct |
20 |
Correct |
7 ms |
2048 KB |
Output is correct |
21 |
Correct |
7 ms |
2048 KB |
Output is correct |
22 |
Correct |
76 ms |
14184 KB |
Output is correct |
23 |
Correct |
82 ms |
14316 KB |
Output is correct |
24 |
Correct |
76 ms |
14184 KB |
Output is correct |
25 |
Correct |
78 ms |
14180 KB |
Output is correct |
26 |
Correct |
79 ms |
14184 KB |
Output is correct |
27 |
Correct |
81 ms |
14180 KB |
Output is correct |
28 |
Correct |
81 ms |
14180 KB |
Output is correct |
29 |
Correct |
77 ms |
14180 KB |
Output is correct |
30 |
Correct |
81 ms |
14308 KB |
Output is correct |
31 |
Correct |
76 ms |
14180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
21472 KB |
Output is correct |
2 |
Correct |
159 ms |
21472 KB |
Output is correct |
3 |
Correct |
167 ms |
21472 KB |
Output is correct |
4 |
Correct |
158 ms |
21432 KB |
Output is correct |
5 |
Correct |
159 ms |
21476 KB |
Output is correct |
6 |
Correct |
161 ms |
21472 KB |
Output is correct |
7 |
Correct |
157 ms |
21472 KB |
Output is correct |
8 |
Correct |
149 ms |
21472 KB |
Output is correct |
9 |
Correct |
161 ms |
21636 KB |
Output is correct |
10 |
Correct |
146 ms |
21468 KB |
Output is correct |
11 |
Correct |
154 ms |
21472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2048 KB |
Output is correct |
2 |
Correct |
7 ms |
2048 KB |
Output is correct |
3 |
Correct |
7 ms |
2048 KB |
Output is correct |
4 |
Correct |
7 ms |
2048 KB |
Output is correct |
5 |
Correct |
7 ms |
2176 KB |
Output is correct |
6 |
Correct |
7 ms |
2048 KB |
Output is correct |
7 |
Correct |
7 ms |
2048 KB |
Output is correct |
8 |
Correct |
7 ms |
2048 KB |
Output is correct |
9 |
Correct |
7 ms |
2048 KB |
Output is correct |
10 |
Correct |
7 ms |
2048 KB |
Output is correct |
11 |
Correct |
7 ms |
2048 KB |
Output is correct |
12 |
Correct |
7 ms |
2048 KB |
Output is correct |
13 |
Correct |
7 ms |
2048 KB |
Output is correct |
14 |
Correct |
7 ms |
2048 KB |
Output is correct |
15 |
Correct |
7 ms |
2048 KB |
Output is correct |
16 |
Correct |
7 ms |
2048 KB |
Output is correct |
17 |
Correct |
7 ms |
2048 KB |
Output is correct |
18 |
Correct |
7 ms |
2048 KB |
Output is correct |
19 |
Correct |
7 ms |
2048 KB |
Output is correct |
20 |
Correct |
7 ms |
2048 KB |
Output is correct |
21 |
Correct |
7 ms |
2048 KB |
Output is correct |
22 |
Correct |
76 ms |
14184 KB |
Output is correct |
23 |
Correct |
82 ms |
14316 KB |
Output is correct |
24 |
Correct |
76 ms |
14184 KB |
Output is correct |
25 |
Correct |
78 ms |
14180 KB |
Output is correct |
26 |
Correct |
79 ms |
14184 KB |
Output is correct |
27 |
Correct |
81 ms |
14180 KB |
Output is correct |
28 |
Correct |
81 ms |
14180 KB |
Output is correct |
29 |
Correct |
77 ms |
14180 KB |
Output is correct |
30 |
Correct |
81 ms |
14308 KB |
Output is correct |
31 |
Correct |
76 ms |
14180 KB |
Output is correct |
32 |
Correct |
163 ms |
21472 KB |
Output is correct |
33 |
Correct |
159 ms |
21472 KB |
Output is correct |
34 |
Correct |
167 ms |
21472 KB |
Output is correct |
35 |
Correct |
158 ms |
21432 KB |
Output is correct |
36 |
Correct |
159 ms |
21476 KB |
Output is correct |
37 |
Correct |
161 ms |
21472 KB |
Output is correct |
38 |
Correct |
157 ms |
21472 KB |
Output is correct |
39 |
Correct |
149 ms |
21472 KB |
Output is correct |
40 |
Correct |
161 ms |
21636 KB |
Output is correct |
41 |
Correct |
146 ms |
21468 KB |
Output is correct |
42 |
Correct |
154 ms |
21472 KB |
Output is correct |
43 |
Correct |
178 ms |
22240 KB |
Output is correct |
44 |
Correct |
161 ms |
22112 KB |
Output is correct |
45 |
Correct |
165 ms |
22108 KB |
Output is correct |
46 |
Correct |
158 ms |
22112 KB |
Output is correct |
47 |
Correct |
151 ms |
22116 KB |
Output is correct |
48 |
Correct |
150 ms |
21988 KB |
Output is correct |
49 |
Correct |
149 ms |
22112 KB |
Output is correct |
50 |
Correct |
160 ms |
22112 KB |
Output is correct |
51 |
Correct |
148 ms |
22112 KB |
Output is correct |