E. Tree Queries 子树编号()

博客探讨了在深度优先搜索(DFS)过程中如何利用l和r数组确定节点所在子树的最小和最大编号。通过遍历子节点,可以确定每个节点的子树编号范围。
摘要由CSDN通过智能技术生成

dfs中引用l r这两个数组可以来说明该节点所在的子树的最小和最大编号
很明显 本身就是最小的编号 最大的编号就是访问完所有的子节点之后的编号

//MADE BY Y_is_sunshine;
//#include <bits/stdc++.h>
//#include <memory.h>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>

#define pb push_back
#define INF 0x3f3f3f3f
#define MAXN (int) 2e5 + 7

typedef long long ll;

const ll mod = 998244353;
const double PI = acos(-1);

using namespace std;

int N, M, K;

vector<int> g[MAXN];

int f[MAXN];
int l[MAXN];
int r[MAXN];
int cnt;
int cnt_2;

void dfs(int u, int fa) {

	l[u] = ++cnt_2;
	for (auto it : g[u]) {
		int v = it;
		if (v == fa)
			continue;
		f[v] = u;
		dfs(v, u);

	}
	r[u] = ++cnt;
}

int cmp(int a, int b){
	return l[a] < l[b];
}

int main(void)
{
#ifdef Sunshine
	freopen("data.txt", "r", stdin);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int T;
	cin >> N >> T;
	for (int i = 1; i < N; i++) {
		int u, v;
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}

	dfs(1, -1);

	f[1] = 1;
	while (T--) {
		cin >> M;
		vector<int> v1;
		while (M--) {
			int a;
			cin >> a;
			v1.pb(f[a]);
		}

		sort(v1.begin(), v1.end(), cmp);

		int _min, _max;
		_min = 0;
		_max = N;

		bool ok = true;
		for (int i = 0; i < v1.size(); i++) {
			if (_min <= l[v1[i]] && _max >= r[v1[i]]) {
				_min = l[v1[i]];
				_max = r[v1[i]];
 			}
			else ok = false;
		}

		if (ok)
			cout << "YES\n";
		else
			cout << "NO\n";



	}






#ifdef Sunshine
	freopen("CON", "r", stdin);
	system("pause");
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值