A14883 | Words on Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree consisting of $n$ vertices, and $q$ triples $(x_i, y_i, s_i)$ , where $x_i$ and $y_i$ are integers from $1$ to $n$ , and $s_i$ is a string with length equal to the number of vertices on the simple path from $x_i$ to $y_i$ .
You want to write a lowercase Latin letter on each vertex in such a way that, for each of $q$ given triples, at least one of the following conditions holds:
- if you write out the letters on the vertices on the simple path from $x_i$ to $y_i$ in the order they appear on this path, you get the string $s_i$ ;
- if you write out the letters on the vertices on the simple path from $y_i$ to $x_i$ in the order they appear on this path, you get the string $s_i$ .
Find any possible way to write a letter on each vertex to meet these constraints, or report that it is impossible.
You want to write a lowercase Latin letter on each vertex in such a way that, for each of $q$ given triples, at least one of the following conditions holds:
- if you write out the letters on the vertices on the simple path from $x_i$ to $y_i$ in the order they appear on this path, you get the string $s_i$ ;
- if you write out the letters on the vertices on the simple path from $y_i$ to $x_i$ in the order they appear on this path, you get the string $s_i$ .
Find any possible way to write a letter on each vertex to meet these constraints, or report that it is impossible.
输入格式
The first line contains two integers $n$ and $q$ ( $2 \le n \le 4 \cdot 10^5$ ; $1 \le q \le 4 \cdot 10^5$ ) — the number of vertices in the tree and the number of triples, respectively.
Then $n - 1$ lines follow; the $i$ -th of them contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ ; $u_i \ne v_i$ ) — the endpoints of the $i$ -th edge. These edges form a tree.
Then $q$ lines follow; the $j$ -th of them contains two integers $x_j$ and $y_j$ , and a string $s_j$ consisting of lowercase Latin letters. The length of $s_j$ is equal to the number of vertices on the simple path between $x_j$ and $y_j$ .
Additional constraint on the input: $\sum \limits_{j=1}^{q} |s_j| \le 4 \cdot 10^5$ .
Then $n - 1$ lines follow; the $i$ -th of them contains two integers $u_i$ and $v_i$ ( $1 \le u_i, v_i \le n$ ; $u_i \ne v_i$ ) — the endpoints of the $i$ -th edge. These edges form a tree.
Then $q$ lines follow; the $j$ -th of them contains two integers $x_j$ and $y_j$ , and a string $s_j$ consisting of lowercase Latin letters. The length of $s_j$ is equal to the number of vertices on the simple path between $x_j$ and $y_j$ .
Additional constraint on the input: $\sum \limits_{j=1}^{q} |s_j| \le 4 \cdot 10^5$ .
输出格式
If there is no way to meet the conditions on all triples, print NO. Otherwise, print YES in the first line, and a string of $n$ lowercase Latin letters in the second line; the $i$ -th character of the string should be the letter you write on the $i$ -th vertex. If there are multiple answers, print any of them.
输入输出样例
输入 #1
3 2 2 3 2 1 2 1 ab 2 3 bc
输出 #1
YES abc
输入 #2
3 2 2 3 2 1 2 1 ab 2 3 cd
输出 #2
NO
输入 #3
10 10 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 2 ab 1 3 ab 1 4 ab 1 5 ab 1 6 ab 1 7 ab 1 8 ab 1 9 ab 1 10 ab 10 2 aba
输出 #3
YES baaaaaaaaa
输入 #4
10 10 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 2 ab 1 3 ab 1 4 aa 1 5 ab 1 6 ab 1 7 ab 1 8 ab 1 9 ab 1 10 ab 10 2 aba
输出 #4
NO
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted