570D - Tree Requests

本文介绍了一种解决特定类型查询问题的方法——利用树形动态规划(树形DP)结合DSU-On-Tree技术。具体地,文章探讨了如何在树状结构中高效处理关于子树内节点深度及字符分布的查询,并通过一个实例问题进行说明。

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.

Input
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.

Output
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).

Examples
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”.

题意:让你在以root 为父节点的子树中挑出所有深度为d的点问这些节点上的字母经过任意排列是不是可以形成一个回文。
思路:由于是任意组合,那么只要这个单词数量是偶数就必定可以,如果奇数个数的单词数不超过1个,那么也是可以的。由于是离线查询,所以可以想到用dsu on tree 去统计树上节点的属性,这个属性就是深度为d各个单词的数量。。。我把以u节点的所有深度存下来再去查询错了。。把所有查询先读下来边统计边处理查询再一次输出对了。。。还是玄学。。。

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<map>
using namespace std;

//thanks to pyf ...

#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
typedef pair<int, int> PII;
typedef long long ll;

const int N = 500000 + 5;

struct Edge
{
    int u, v, next;
} edge[N * 2];

char cap[N];
int cur_deep_cnt[N];
int cur_deep_dif_cnt[N][30];
int cur_status[N];
int ans[N];
vector<PII>Q[N];
int deep[N];
int clr[N];
int big[N];
int head[N];
int sz[N];
int tot;
int Max_d = 0;
void init()
{
    CLR(cur_deep_cnt, 0);
    CLR(cur_deep_dif_cnt, 0);
    CLR(big, 0);
    CLR(head, -1);
    CLR(sz, 0);
    CLR(clr, 0);
    tot = 0;
    Max_d = 0;
}
void add_edge(int u, int v)
{
    edge[tot].u = u;
    edge[tot].v = v;
    edge[tot].next = head[u] ;
    head[u] = tot ++ ;
}
void get_sz(int u, int fa, int d)
{
    sz[u] = 1;
    deep[u] = d;
    Max_d = max(Max_d, d);
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (v == fa)
            continue;
        get_sz(v, u, d + 1);
        sz[u] += sz[v];
    }
}
// void add(int u, int fa)
// {
//  int val = cap[u] - 'a';
//  cur_deep_cnt[deep[u]] -= cur_deep_dif_cnt[deep[u]][val];
//  cur_deep_dif_cnt[deep[u]][val] ^= 1;
//  cur_deep_cnt[deep[u]] += cur_deep_dif_cnt[deep[u]][val];
//  for (int i = head[u]; i != -1; i = edge[i].next)
//  {
//      int v = edge[i].v;
//      if (fa == v || big[v])
//          continue;
//      add(v, u);
//  }
// }
void add(int u, int fa)
{
    cur_status[deep[u]] ^= (1 << (cap[u] - 'a'));
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (big[v] || v == fa)
            continue;
        add(v, u);
    }
}
void dfs(int u, int fa, bool keep)
{
    int mx = -1, big_son = -1;
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if (v == fa)
            continue;
        if (sz[v] > mx)
            mx = sz[v] , big_son = v;
    }
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        if (edge[i].v != fa && edge[i].v != big_son)
        {
            dfs(edge[i].v, u, 0);
        }
    }
    if (big_son != -1)
        dfs(big_son, u, 1), big[big_son] = 1;
    add(u, fa);
    for (int i = 0; i < Q[u].size(); i++)
    {
        int id = Q[u][i].second;
        int dep = Q[u][i].first;
        //  ans[id] = (cur_deep_cnt[dep] > 1);
        ans[id] = (cur_status[dep] & (cur_status[dep] - 1)) > 1;
    }
    if (big_son != -1)
        big[big_son] = 0;
    if (!keep)
        add(u, fa);
}

int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        init();
        for (int i = 2; i <= n; i++)
        {
            int u;
            scanf("%d", &u);
            add_edge(u, i);
//          add_edge(i, u);
        }
        for (int i = 1; i <= n; i++)
            cin >> cap[i];
        // for (int i = 1; i <= n; i++)
        // {
        //  for (int j = 1; j <= Max_d; j++)
        //  {
        //      cout << ans[i][j - 1] << " ";
        //  }
        //  cout << endl;
        // }
        for (int i = 0; i < m; i++)
        {
            int r, d;
            scanf("%d%d", &r, &d);
            Q[r].push_back(mp(d, i));
        }
        get_sz(1, 1, 1);
        dfs(1, 1, 0);
        for (int i = 0; i < m; i++)
            if (ans[i])
                printf("No\n");
            else
                printf("Yes\n");
    }
}
<think>好的,我现在需要处理用户关于DMA请求配置的问题,特别是当设备树中dma-requests设置为48时可能遇到的问题。首先,我应该回顾DMA的基本概念和工作原理。DMA(直接内存访问)允许外设直接与内存交互,无需CPU介入,从而提升系统效率[^1]。在设备树中配置DMA请求时,关键参数包括通道数、请求线数量和优先级设置。 用户提到dma-requests=48,这可能指的是某个DMA控制器支持的请求线数量。我需要考虑几个方面:硬件限制、设备树配置的正确性以及驱动兼容性。首先,检查DMA控制器的数据手册,确认其实际支持的请求线数目是否确实为48。如果硬件不支持这么多,配置错误会导致DMA无法正常工作[^2]。 接下来,设备树中的配置需要与硬件匹配。例如,在Linux内核中,DMA控制器的设备树节点通常会指定dma-cells属性,而外设节点会通过dmas和dma-names来引用这些控制器。如果dma-requests设置为48,但驱动代码中未正确解析该数值,可能导致资源分配错误或溢出。 此外,中断处理和内存分配也是需要注意的地方。高数量的DMA请求可能需要更多的中断向量或更大的内存缓冲区,如果系统资源不足,可能会引发竞争条件或数据丢失。同时,不同DMA通道之间的优先级配置不当,可能导致低优先级请求被长时间阻塞。 调试方面,建议用户检查内核启动日志中的DMA初始化信息,使用dmaengine的调试工具,以及验证设备树编译后的dtb文件是否正确。如果问题依旧,可能需要深入分析驱动代码,确认是否有硬编码限制或配置解析错误。 最后,需要确保引用的资料准确。例如,引用[1]提到内存划分支持多道程序,虽然与DMA相关,但更直接的引用应关注DMA控制器文档或内核文档中的相关部分。用户提供的引用[2]关于网络连接速度,可能与当前问题无关,应避免引用不相关的内容。</think>### DMA请求配置与问题排查指南 #### 一、DMA请求配置核心要素 1. **硬件资源映射**:DMA控制器的物理地址和中断号需在设备树中准确声明,例如: ```dts dma_controller: dma-controller@48000000 { compatible = "ti,omap-dma"; reg = <0x48000000 0x1000>; dma-requests = <48>; #dma-cells = <1>; interrupts = <32>; }; ``` 2. **请求线分配规则**: - 每个外设通过`dmas = <&dma_controller 12>`指定请求线编号 - 48个请求线需按硬件手册划分用途(如:0-15给UART,16-31给SPI) 3. **通道优先级配置**: ```dts dma_channel { assigned-dma-request-lines = <0 47>; dma-channel-mask = <0xffffffff>; priority = <15>; // 0-15级优先级 }; ``` #### 二、dma-requests=48的典型问题 1. **硬件支持验证**: - 检查芯片手册确认DMA控制器物理请求线数量 - 验证SoC封装是否引出全部48条请求线 2. **驱动兼容性检查**: ```c // 驱动代码中需匹配请求数声明 if (dma_spec->args[0] >= 48) { dev_err(dev, "Invalid DMA request line %u\n", dma_spec->args[0]); return -EINVAL; } ``` 3. **资源冲突排查**: - 使用`dmatest`内核模块验证通道可用性 - 通过`/sys/kernel/debug/dmaengine/`查看请求线占用状态 #### 三、调试方法与工具 1. **设备树编译验证**: ```bash dtc -I dtb -O dts /proc/device-tree | grep dma-requests ``` 2. **内核日志分析**: ```dmesg [ 2.385710] dma-pl330 48000000.dma-controller: DMA: 48 channels, 48 requests ``` 3. **性能监测**: ```bash perf record -e dma_fifo:* ``` #### 四、典型解决方案 1. **请求线复用配置**: ```dts shared_dma_line { dmas = <&dma_controller 12>, <&dma_controller 35>; dma-names = "tx", "rx"; }; ``` 2. **动态分配策略**: ```c dma_cap_set(DMA_CYCLIC, mask); dma_req = dma_request_chan(dev, "rx"); ``` 3. **错误处理增强**: ```c if (dmaengine_submit(desc) < 0) { dmaengine_terminate_sync(chan); pr_warn("DMA submit failed on req %d", req_id); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值