A8809 | Little Elephant and Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The Little Elephant loves trees very much, he especially loves root trees.
He's got a tree consisting of $n$ nodes (the nodes are numbered from 1 to $n$ ), with root at node number $1$ . Each node of the tree contains some list of numbers which initially is empty.
The Little Elephant wants to apply $m$ operations. On the $i$ -th operation $(1<=i<=m)$ he first adds number $i$ to lists of all nodes of a subtree with the root in node number $a_{i}$ , and then he adds number $i$ to lists of all nodes of the subtree with root in node $b_{i}$ .
After applying all operations the Little Elephant wants to count for each node $i$ number $c_{i}$ — the number of integers $j$ $(1<=j<=n; j≠i)$ , such that the lists of the $i$ -th and the $j$ -th nodes contain at least one common number.
Help the Little Elephant, count numbers $c_{i}$ for him.
He's got a tree consisting of $n$ nodes (the nodes are numbered from 1 to $n$ ), with root at node number $1$ . Each node of the tree contains some list of numbers which initially is empty.
The Little Elephant wants to apply $m$ operations. On the $i$ -th operation $(1<=i<=m)$ he first adds number $i$ to lists of all nodes of a subtree with the root in node number $a_{i}$ , and then he adds number $i$ to lists of all nodes of the subtree with root in node $b_{i}$ .
After applying all operations the Little Elephant wants to count for each node $i$ number $c_{i}$ — the number of integers $j$ $(1<=j<=n; j≠i)$ , such that the lists of the $i$ -th and the $j$ -th nodes contain at least one common number.
Help the Little Elephant, count numbers $c_{i}$ for him.
输入格式
The first line contains two integers $n$ and $m$ $(1<=n,m<=10^{5})$ — the number of the tree nodes and the number of operations.
Each of the following $n-1$ lines contains two space-separated integers, $u_{i}$ and $v_{i}$ $(1<=u_{i},v_{i}<=n,u_{i}≠v_{i})$ , that mean that there is an edge between nodes number $u_{i}$ and $v_{i}$ .
Each of the following $m$ lines contains two space-separated integers, $a_{i}$ and $b_{i}$ $(1<=a_{i},b_{i}<=n,a_{i}≠b_{i})$ , that stand for the indexes of the nodes in the $i$ -th operation.
It is guaranteed that the given graph is an undirected tree.
Each of the following $n-1$ lines contains two space-separated integers, $u_{i}$ and $v_{i}$ $(1<=u_{i},v_{i}<=n,u_{i}≠v_{i})$ , that mean that there is an edge between nodes number $u_{i}$ and $v_{i}$ .
Each of the following $m$ lines contains two space-separated integers, $a_{i}$ and $b_{i}$ $(1<=a_{i},b_{i}<=n,a_{i}≠b_{i})$ , that stand for the indexes of the nodes in the $i$ -th operation.
It is guaranteed that the given graph is an undirected tree.
输出格式
In a single line print $n$ space-separated integers — $c_{1},c_{2},...,c_{n}$ .
输入输出样例
输入 #1
5 1 1 2 1 3 3 5 3 4 2 3
输出 #1
0 3 3 3 3
输入 #2
11 3 1 2 2 3 2 4 1 5 5 6 5 7 5 8 6 9 8 10 8 11 2 9 3 6 2 8
输出 #2
0 6 7 6 0 2 0 5 4 5 5
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted