A15820 | A Wide, Wide Graph
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a tree (a connected graph without cycles) with $n$ vertices.
Consider a fixed integer $k$ . Then, the graph $G_k$ is an undirected graph with $n$ vertices, where an edge between vertices $u$ and $v$ exists if and only if the distance between vertices $u$ and $v$ in the given tree is at least $k$ .
For each $k$ from $1$ to $n$ , print the number of connected components in the graph $G_k$ .
Consider a fixed integer $k$ . Then, the graph $G_k$ is an undirected graph with $n$ vertices, where an edge between vertices $u$ and $v$ exists if and only if the distance between vertices $u$ and $v$ in the given tree is at least $k$ .
For each $k$ from $1$ to $n$ , print the number of connected components in the graph $G_k$ .
输入格式
The first line contains the integer $n$ ( $2 \le n \le 10^5$ ) — the number of vertices in the graph.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ ), denoting an edge between vertices $u$ and $v$ in the tree. It is guaranteed that these edges form a valid tree.
Each of the next $n-1$ lines contains two integers $u$ and $v$ ( $1 \le u, v \le n$ ), denoting an edge between vertices $u$ and $v$ in the tree. It is guaranteed that these edges form a valid tree.
输出格式
Output $n$ integers: the number of connected components in the graph $G_k$ for each $k$ from $1$ to $n$ .
输入输出样例
输入 #1
6 1 2 1 3 2 4 2 5 3 6
输出 #1
1 1 2 4 6 6
输入 #2
5 1 2 2 3 3 4 3 5
输出 #2
1 1 3 5 5
In the first example: If $k=1$ , the graph has an edge between each pair of vertices, so it has one component. If $k=4$ , the graph has only edges $4 \leftrightarrow 6$ and $5 \leftrightarrow 6$ , so the graph has $4$ components.
In the second example: when $k=1$ or $k=2$ the graph has one component. When $k=3$ the graph $G_k$ splits into $3$ components: one component has vertices $1$ , $4$ and $5$ , and two more components contain one vertex each. When $k=4$ or $k=5$ each vertex is a separate component.
In the second example: when $k=1$ or $k=2$ the graph has one component. When $k=3$ the graph $G_k$ splits into $3$ components: one component has vertices $1$ , $4$ and $5$ , and two more components contain one vertex each. When $k=4$ or $k=5$ each vertex is a separate component.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted