CF1657F.Words on Tree
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a tree consisting of n vertices, and q triples (xi,yi,si) , where xi and yi are integers from 1 to n , and si is a string with length equal to the number of vertices on the simple path from xi to yi .
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 xi to yi in the order they appear on this path, you get the string si ;
- if you write out the letters on the vertices on the simple path from yi to xi in the order they appear on this path, you get the string si .
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≤n≤4⋅105 ; 1≤q≤4⋅105 ) — 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 ui and vi ( 1≤ui,vi≤n ; ui=vi ) — the endpoints of the i -th edge. These edges form a tree.
Then q lines follow; the j -th of them contains two integers xj and yj , and a string sj consisting of lowercase Latin letters. The length of sj is equal to the number of vertices on the simple path between xj and yj .
Additional constraint on the input: j=1∑q∣sj∣≤4⋅105 .
输出格式
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