CF1657F.Words on Tree

省选/NOI-

通过率:0%

时间限制:9.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a tree consisting of nn vertices, and qq triples (xi,yi,si)(x_i, y_i, s_i), where xix_i and yiy_i are integers from 11 to nn, and sis_i is a string with length equal to the number of vertices on the simple path from xix_i to yiy_i.

You want to write a lowercase Latin letter on each vertex in such a way that, for each of qq given triples, at least one of the following conditions holds:

  • if you write out the letters on the vertices on the simple path from xix_i to yiy_i in the order they appear on this path, you get the string sis_i;
  • if you write out the letters on the vertices on the simple path from yiy_i to xix_i in the order they appear on this path, you get the string sis_i.

Find any possible way to write a letter on each vertex to meet these constraints, or report that it is impossible.

给你一棵包含 nn 个顶点的树,以及 qq 个三元组 (xi,yi,si)(x_i, y_i, s_i),其中 xix_i 和 yiy_i 是介于 11 到 nn 之间的整数,sis_i 是一个字符串,其长度等于从 xix_i 到 yiy_i 的简单路径上的顶点数目。

你需要在每个顶点上写一个小写拉丁字母,使得对每个给定的三元组,以下两个条件中至少有一个成立:

  • 若将从 xix_i 到 yiy_i 的简单路径上各顶点所写字母按路径顺序写出,则得到字符串 sis_i;
  • 若将从 yiy_i 到 xix_i 的简单路径上各顶点所写字母按路径顺序写出,则得到字符串 sis_i。

请找出一种满足上述约束的顶点赋字母方案;若不存在这样的方案,则报告无解。

输入格式

The first line contains two integers nn and qq (2≤n≤4⋅1052 \le n \le 4 \cdot 10^5; 1≤q≤4⋅1051 \le q \le 4 \cdot 10^5) — the number of vertices in the tree and the number of triples, respectively.

Then n−1n - 1 lines follow; the ii-th of them contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \le u_i, v_i \le n; ui≠viu_i \ne v_i) — the endpoints of the ii-th edge. These edges form a tree.

Then qq lines follow; the jj-th of them contains two integers xjx_j and yjy_j, and a string sjs_j consisting of lowercase Latin letters. The length of sjs_j is equal to the number of vertices on the simple path between xjx_j and yjy_j.

Additional constraint on the input: ∑j=1q∣sj∣≤4⋅105\sum \limits_{j=1}^{q} |s_j| \le 4 \cdot 10^5.

第一行包含两个整数 nn 和 qq(2≤n≤4⋅1052 \le n \le 4 \cdot 10^5;1≤q≤4⋅1051 \le q \le 4 \cdot 10^5)—— 分别表示树中顶点的数量和三元组的数量。

接下来是 n−1n - 1 行;其中第 ii 行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \le u_i, v_i \le n;ui≠viu_i \ne v_i)—— 表示第 ii 条边的两个端点。这些边构成一棵树。

接下来是 qq 行;其中第 jj 行包含两个整数 xjx_j 和 yjy_j,以及一个由小写拉丁字母组成的字符串 sjs_j。字符串 sjs_j 的长度等于 xjx_j 与 yjy_j 之间简单路径上的顶点数。

输入的额外约束:∑j=1q∣sj∣≤4⋅105\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 nn lowercase Latin letters in the second line; the ii-th character of the string should be the letter you write on the ii-th vertex. If there are multiple answers, print any of them.

如果无法满足所有三元组的条件,则输出 NO。否则,第一行输出 YES,第二行输出一个长度为 nn 的小写拉丁字母字符串;该字符串的第 ii 个字符即为写在第 ii 个顶点上的字母。若存在多个合法答案,输出任意一个即可。

输入输出样例

  • 输入#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

输入解题思路,AI测评打分。不知道怎么写?

首页