zoukankan      html  css  js  c++  java
  • Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    题目链接:

    题目

    D. Tree Requests
    time limit per test:2 seconds
    memory limit per test:256 megabytes

    问题描述

    Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the n - 1 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex i is vertex pi, the parent index is always less than the index of the vertex (i.e., pi < i).

    The depth of the vertex is the number of nodes on the path from the root to v along the edges. In particular, the depth of the root is equal to 1.

    We say that vertex u is in the subtree of vertex v, if we can get from u to v, moving from the vertex to the parent. In particular, vertex v is in its subtree.

    Roma gives you m queries, the i-th of which consists of two numbers vi, hi. Let's consider the vertices in the subtree vi located at depth hi. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used.

    输入

    The first line contains two integers n, m (1 ≤ n, m ≤ 500 000) — the number of nodes in the tree and queries, respectively.

    The following line contains n - 1 integers p2, p3, ..., pn — the parents of vertices from the second to the n-th (1 ≤ pi < i).

    The next line contains n lowercase English letters, the i-th of these letters is written on vertex i.

    Next m lines describe the queries, the i-th line contains two numbers vi, hi (1 ≤ vi, hi ≤ n) — the vertex and the depth that appear in the i-th query.

    输出

    Print m lines. In the i-th line print "Yes" (without the quotes), if in the i-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes).

    样例

    input
    6 5
    1 1 1 3 3
    zacccd
    1 1
    3 3
    4 1
    6 1
    1 2

    output
    Yes
    No
    Yes
    Yes
    Yes

    Note

    String s is a palindrome if reads the same from left to right and from right to left. In particular, an empty string is a palindrome.

    Clarification for the sample test.

    In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z".

    In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d" respectively. It is impossible to form a palindrome of them.

    In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome.

    In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome.

    In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c" and "c". We may form a palindrome "cac".

    题意

    给你一颗有根树(根节点为1),每个节点有一个字母,回答m个询问,每个询问有两个变量v,h,求深度为h的并且为以v为根的子树的所有节点能否构成回文串(可以任意排列)

    题解

    回文串的判定:当出现为奇数的字母不超过1个的时候,可以构成回文串。
    字母的存储:我们可以把26个字母压到二进制数的26位

    1、离线:
    开ans[]数组,ans[i]存放第i次查询的结果。
    开pre[]数组,pre[h]表示深度为i的在dfs过程中当前已经访问过的所有节点的字母的异或值,那么对于第i次询问v,h我们只要在开始访问(前序遍历到)v的时候,ans[i]=pre[h]。在结束访问(后续遍历到)v的时候ans[i]=pre[h],那么,我们就得到第i次询问的结果了!!!

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #define X first
    #define Y second
    #define mp make_pair
    using namespace std;
    
    const int maxn = 5e5 + 10;
    
    vector<int> G[maxn];
    vector<pair<int, int> > que[maxn];
    char str[maxn];
    int pre[maxn],ans[maxn];
    int n, m;
    
    void dfs(int u, int dep) {
    	for (int i = 0; i < que[u].size(); i++) {
    		ans[que[u][i].X] ^= pre[que[u][i].Y];
    	}
    	for (int i = 0; i < G[u].size(); i++) {
    		int v = G[u][i];
    		dfs(v, dep + 1);
    	}
    	pre[dep] ^= (1 << (str[u] - 'a'));
    	for (int i = 0; i < que[u].size(); i++) {
    		ans[que[u][i].X] ^= pre[que[u][i].Y];
    	}
    }
    
    void init() {
    	memset(pre, 0, sizeof(pre));
    	memset(ans, 0, sizeof(ans));
    }
    
    int main() {
    	init();
    	scanf("%d%d", &n, &m);
    	for (int i = 2; i <= n; i++) {
    		int x; scanf("%d", &x);
    		G[x].push_back(i);
    	}
    	scanf("%s", str + 1);
    	for (int i = 0; i < m; i++) {
    		int v, h; scanf("%d%d", &v, &h);
    		que[v].push_back(mp(i, h));
    	}
    	dfs(1, 1);
    	for (int i = 0; i < m; i++) {
    		if (ans[i] & (ans[i] - 1)) {
    			puts("No");
    		}
    		else puts("Yes");
    	}
    	return 0;
    }
    

    2、在线:
    首先,我们求树的dfs序,in[maxn]和out[maxn];
    我们开vector depth[d]保存所有在深度d的字母的前缀异或和(把每个字母所在的节点的out[]序也丢进去。 (注意:out[]值小的节点一定会先扔进去)
    这样,当要处理查询v,h的时候,对于v的子树节点,它们的out序在depth[h]里面必定是连续的一段,我们只要通过二分就能找出这一段的开始和终止的位置(注意这里的开始是指开始访问的前一个)那么我们只要异或一下开始和终止的位置的,得到的就是我们要的结果了。

    对于得到的结果ans,ans&(ans-1)就可以判断1的个数是否<=1了。

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<algorithm>
    #define X first
    #define Y second
    #define mp make_pair
    using namespace std;
    
    const int maxn = 5e5 + 10;
    
    vector<int> G[maxn];
    vector<pair<int, int> > dep[maxn];
    char str[maxn];
    int ans[maxn];
    int n, m;
    
    int in[maxn], out[maxn],dfs_clock;
    void dfs(int u, int d) {
    	in[u] = dfs_clock++;
    	for (int i = 0; i < G[u].size(); i++) {
    		int v = G[u][i];
    		dfs(v, d + 1);
    	}
    	dep[d].push_back(mp(dfs_clock,dep[d].back().Y^(1<<(str[u]-'a'))));
    	out[u] = dfs_clock++;
    }
    
    void init() {
    	memset(ans, 0, sizeof(ans));
    	dfs_clock = 1;
    	for (int i = 0; i < maxn; i++) {
    		dep[i].push_back(mp(0, 0));
    	}
    }
    
    int main() {
    	init();
    	scanf("%d%d", &n, &m);
    	for (int i = 2; i <= n; i++) {
    		int x; scanf("%d", &x);
    		G[x].push_back(i);
    	}
    	scanf("%s", str + 1);
    	dfs(1, 1);
    	for (int i = 0; i < m; i++) {
    		int v, d; scanf("%d%d", &v, &d);
    		int l = lower_bound(dep[d].begin(), dep[d].end(), mp(in[v], -1)) - dep[d].begin() - 1;
    		int r = lower_bound(dep[d].begin(), dep[d].end(), mp(out[v], -1)) - dep[d].begin() - 1;
    		ans[i] = dep[d][l].Y ^ dep[d][r].Y;
    		if (ans[i] & (ans[i] - 1)) puts("No");
    		else puts("Yes");
    	}
    	return 0;
    }
  • 相关阅读:
    duilib clabelui 属性
    C++ 时间戳和时间的互相转换
    VS提示已有主体显示 的error解决办法
    Webrtc createoffer以后没有 onsuccess的回调
    VS error LNK2019: 无法解析的外部符号 _AcquireCredentialsHandleA
    VS LNK2019 无法解析的外部符号_GetAdaptersAddresses@20 错误解决办法
    duilib xml布局文件的属性设置
    Doris安装
    Oozie初探
    Notepad++ 常规骚操作
  • 原文地址:https://www.cnblogs.com/fenice/p/5671859.html
Copyright © 2011-2022 走看看