//https://vjudge.csgrandeur.cn/contest/523128#problem/G
#include<bits/stdc++.h>
#include<unordered_map>
#include<array>
#define ll long long
#define ull unsigned long long
#define all(a) a.begin(),a.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-4;
const ll mod = 998244353;
const int N = 1e5 + 5;
int n, m, fa[N], l[N], r[N], tot;
struct Op {
int op, x, y;
}q[N];
vector<int> g[N];
pair<int, int> p[N];
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y)
{
x = find(x);
y = find(y);
fa[x] = y;
}
void dfs(int x)
{
l[x] = ++tot;
for (auto to : g[x])
dfs(to);
r[x] = tot;
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> q[i].op;
if (q[i].op == 2)
cin >> q[i].x;
else
cin >> q[i].x >> q[i].y;
}
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1, num = 0; i <= m; i++)
{
if (q[i].op == 1)
{
g[q[i].y].push_back(q[i].x);
merge(q[i].x, q[i].y);
}
else if (q[i].op == 2)
p[++num] = { q[i].x, find(q[i].x) };
}
for (int i = 1; i <= n; i++)
if (find(i) == i)
dfs(i);
for (int i = 1; i <= m; i++)
{
if (q[i].op != 3)
continue;
int up = p[q[i].y].second;
int down = p[q[i].y].first;
if (l[q[i].x] >= l[up] && l[q[i].x] <= l[down] && r[q[i].x] <= r[up] && r[q[i].x] >= r[down])//判断是否在路径中,先进后出的原则
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
}
signed main()
{
IOS;
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
dfs序判断一个点是否在树上的一条路径中(知道路径的两个端点)
最新推荐文章于 2023-02-26 20:22:18 发布