A16275 | Beautiful Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Lunchbox has a tree of size $n$ rooted at node $1$ . Each node is then assigned a value. Lunchbox considers the tree to be beautiful if each value is distinct and ranges from $1$ to $n$ . In addition, a beautiful tree must also satisfy $m$ requirements of $2$ types:
- "1 a b c" — The node with the smallest value on the path between nodes $a$ and $b$ must be located at $c$ .
- "2 a b c" — The node with the largest value on the path between nodes $a$ and $b$ must be located at $c$ .
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 a b c" — The node with the smallest value on the path between nodes $a$ and $b$ must be located at $c$ .
- "2 a b c" — The node with the largest value on the path between nodes $a$ and $b$ must be located at $c$ .
Now, you must assign values to each node such that the resulting tree is beautiful. If it is impossible to do so, output $-1$ .
输入格式
The first line contains two integers $n$ and $m$ ( $2 \le n, m \le 2 \cdot 10^5$ ).
The next $n - 1$ lines contain two integers $u$ and $v$ ( $1 \le u, v \le n, u \ne v$ ) — denoting an edge between nodes $u$ and $v$ . It is guaranteed that the given edges form a tree.
The next $m$ lines each contain four integers $t$ , $a$ , $b$ , and $c$ ( $t \in \{1,2\}$ , $1 \le a, b, c \le n$ ). It is guaranteed that node $c$ is on the path between nodes $a$ and $b$ .
The next $n - 1$ lines contain two integers $u$ and $v$ ( $1 \le u, v \le n, u \ne v$ ) — denoting an edge between nodes $u$ and $v$ . It is guaranteed that the given edges form a tree.
The next $m$ lines each contain four integers $t$ , $a$ , $b$ , and $c$ ( $t \in \{1,2\}$ , $1 \le a, b, c \le n$ ). It is guaranteed that node $c$ is on the path between nodes $a$ and $b$ .
输出格式
If it is impossible to assign values such that the tree is beautiful, output $-1$ . Otherwise, output $n$ integers, the $i$ -th of which denotes the value of node $i$ .
输入输出样例
输入 #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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted