A11930 | Ants
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
There is a tree with $n$ vertices. There are also $m$ ants living on it. Each ant has its own color. The $i$ -th ant has two favorite pairs of vertices: ( $a_i, b_i$ ) and ( $c_i, d_i$ ). You need to tell if it is possible to paint the edges of the tree in $m$ 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.
输入格式
The first line contains a single integer $n$ ( $2 \leq n \leq 10^5$ ) — the number of vertices.
Each of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ ), meaning that there is an edge between vertices $u_i$ and $v_i$ .
The next line contains a single integer $m$ ( $1 \leq m \leq 10^4$ ) — the number of ants.
Each of the next $m$ lines contains four integers $a_i$ , $b_i$ , $c_i$ , and $d_i$ ( $1 \leq a_i, b_i, c_i, d_i \leq n$ , $a_i \neq b_i, c_i \neq d_i$ ), meaning that pairs ( $a_i$ , $b_i$ ) and ( $c_i$ , $d_i$ ) are favorite for the $i$ -th ant.
Each of the next $n-1$ lines contains two integers $u_i$ and $v_i$ ( $1 \leq u_i, v_i \leq n$ ), meaning that there is an edge between vertices $u_i$ and $v_i$ .
The next line contains a single integer $m$ ( $1 \leq m \leq 10^4$ ) — the number of ants.
Each of the next $m$ lines contains four integers $a_i$ , $b_i$ , $c_i$ , and $d_i$ ( $1 \leq a_i, b_i, c_i, d_i \leq n$ , $a_i \neq b_i, c_i \neq d_i$ ), meaning that pairs ( $a_i$ , $b_i$ ) and ( $c_i$ , $d_i$ ) are favorite for the $i$ -th ant.
输出格式
Print "NO" (without quotes) if the wanted painting is impossible.
Otherwise, print "YES" (without quotes). Print $m$ lines. On the $i$ -th line, print $1$ if the $i$ -th ant will use the first pair and $2$ otherwise. If there are multiple answers, print any.
Otherwise, print "YES" (without quotes). Print $m$ lines. On the $i$ -th line, print $1$ if the $i$ -th ant will use the first pair and $2$ otherwise. If there are multiple answers, print any.
输入输出样例
输入 #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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted