CF1904F.Beautiful Tree

省选/NOI-

通过率:0%

时间限制:4.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Lunchbox has a tree of size nn rooted at node 11. Each node is then assigned a value. Lunchbox considers the tree to be beautiful if each value is distinct and ranges from 11 to nn. In addition, a beautiful tree must also satisfy mm requirements of 22 types:

  • "1 a b c" — The node with the smallest value on the path between nodes aa and bb must be located at cc.
  • "2 a b c" — The node with the largest value on the path between nodes aa and bb must be located at cc.

Now, you must assign values to each node such that the resulting tree is beautiful. If it is impossible to do so, output −1-1.

Lunchbox 有一棵大小为 nn 的树,根节点为节点 11。每个节点被赋予一个值。Lunchbox 认为这棵树是“优美的”,当且仅当所有节点的值互不相同,且取值范围恰好为 11 到 nn。此外,一棵优美的树还必须满足 mm 个约束条件,分为两类:

  • “1 a b c” —— 节点 aa 与节点 bb 之间路径上值最小的节点必须是节点 cc;
  • “2 a b c” —— 节点 aa 与节点 bb 之间路径上值最大的节点必须是节点 cc。

现在,请你为每个节点赋值,使得最终得到的树是优美的。若无法做到,请输出 −1-1。

输入格式

The first line contains two integers nn and mm (2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5).

The next n−1n - 1 lines contain two integers uu and vv (1≤u,v≤n,u≠v1 \le u, v \le n, u \ne v) — denoting an edge between nodes uu and vv. It is guaranteed that the given edges form a tree.

The next mm lines each contain four integers tt, aa, bb, and cc (t∈1,2t \in {1,2}, 1≤a,b,c≤n1 \le a, b, c \le n). It is guaranteed that node cc is on the path between nodes aa and bb.

第一行包含两个整数 nn 和 mm(2≤n,m≤2⋅1052 \le n, m \le 2 \cdot 10^5)。

接下来的 n−1n - 1 行每行包含两个整数 uu 和 vv(1≤u,v≤n, u≠v1 \le u, v \le n,\ u \ne v),表示节点 uu 与节点 vv 之间存在一条边。保证所给的边构成一棵树。

接下来的 mm 行每行包含四个整数 tt、aa、bb 和 cc(其中 t∈{1,2}t \in \{1,2\},1≤a,b,c≤n1 \le a, b, c \le n)。保证节点 cc 位于节点 aa 与节点 bb 之间的路径上。

输出格式

If it is impossible to assign values such that the tree is beautiful, output −1-1. Otherwise, output nn integers, the ii-th of which denotes the value of node ii.

如果无法为节点赋值使得树是优美的,则输出 −1-1。否则,输出 nn 个整数,其中第 ii 个整数表示节点 ii 的值。

输入输出样例

  • 输入#1

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

    输出#1

    1 6 7 5 3 4 2
  • 输入#2

    2 2
    1 2
    1 1 2 1
    1 1 2 2

    输出#2

    -1

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

首页