CF1007D.Ants

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:768MB

AC君温馨提醒

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

题目描述

There is a tree with nn vertices. There are also mm ants living on it. Each ant has its own color. The ii-th ant has two favorite pairs of vertices: (ai,bia_i, b_i) and (ci,dic_i, d_i). You need to tell if it is possible to paint the edges of the tree in mm colors so that every ant will be able to walk between vertices from one of its favorite pairs using only edges of his color; if it is possible, you need to print which pair every ant should use.

有一棵包含 nn 个顶点的树。树上还生活着 mm 只蚂蚁。每只蚂蚁都有其专属的颜色。第 ii 只蚂蚁有两个偏爱的顶点对:(ai,bi)(a_i, b_i) 和 (ci,di)(c_i, d_i)。你需要判断:是否可以将树的边染成 mm 种颜色,使得每只蚂蚁都能仅通过与其自身颜色相同的边,在其某个偏爱的顶点对之间通行;若可行,则还需输出每只蚂蚁应选择哪一个顶点对。

输入格式

The first line contains a single integer nn (2≤n≤1052 \leq n \leq 10^5) — the number of vertices.

Each of the next n−1n-1 lines contains two integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), meaning that there is an edge between vertices uiu_i and viv_i.

The next line contains a single integer mm (1≤m≤1041 \leq m \leq 10^4) — the number of ants.

Each of the next mm lines contains four integers aia_i, bib_i, cic_i, and did_i (1≤ai,bi,ci,di≤n1 \leq a_i, b_i, c_i, d_i \leq n, ai≠bi,ci≠dia_i \neq b_i, c_i \neq d_i), meaning that pairs (aia_i, bib_i) and (cic_i, did_i) are favorite for the ii-th ant.

第一行包含一个整数 nn(2≤n≤1052 \leq n \leq 10^5)—— 表示顶点的数量。

接下来的 n−1n-1 行,每行包含两个整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n),表示顶点 uiu_i 与 viv_i 之间存在一条边。

下一行包含一个整数 mm(1≤m≤1041 \leq m \leq 10^4)—— 表示蚂蚁的数量。

接下来的 mm 行,每行包含四个整数 aia_i、bib_i、cic_i 和 did_i(1≤ai,bi,ci,di≤n1 \leq a_i, b_i, c_i, d_i \leq n,且 ai≠bia_i \neq b_i,ci≠dic_i \neq d_i),表示第 ii 只蚂蚁的两个喜爱点对为 (ai,bi)(a_i, b_i) 和 (ci,di)(c_i, d_i)。

输出格式

Print "NO" (without quotes) if the wanted painting is impossible.

Otherwise, print "YES" (without quotes). Print mm lines. On the ii-th line, print 11 if the ii-th ant will use the first pair and 22 otherwise. If there are multiple answers, print any.

如果目标涂色方案无法实现,则输出 "NO"(不带引号)。

否则,输出 "YES"(不带引号),并输出 mm 行。在第 ii 行中,若第 ii 只蚂蚁使用第一对颜料,则输出 11,否则输出 22。若存在多种可行方案,输出任意一种即可。

输入输出样例

  • 输入#1

    6
    1 2
    3 1
    4 1
    5 2
    6 2
    3
    2 6 3 4
    1 6 6 5
    1 4 5 2

    输出#1

    YES
    2
    1
    2
  • 输入#2

    5
    1 2
    1 3
    1 4
    1 5
    2
    2 3 4 5
    3 4 5 2

    输出#2

    NO

说明/提示

In the sample, the second and the third edge should be painted in the first color, the first and the fifth should be painted in the second color, and the fourth should be painted in the third color.

在样例中,第二条和第三条边应涂上第一种颜色,第一条和第五条边应涂上第二种颜色,第四条边应涂上第三种颜色。

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

首页