A11122 | Ilya And The Tree
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex $1$ . There is an integer number written on each vertex of the tree; the number written on vertex $i$ is equal to $a_{i}$ .
Ilya believes that the beauty of the vertex $x$ is the greatest common divisor of all numbers written on the vertices on the path from the root to $x$ , including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to $0$ or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
Ilya believes that the beauty of the vertex $x$ is the greatest common divisor of all numbers written on the vertices on the path from the root to $x$ , including this vertex itself. In addition, Ilya can change the number in one arbitrary vertex to $0$ or leave all vertices unchanged. Now for each vertex Ilya wants to know the maximum possible beauty it can have.
For each vertex the answer must be considered independently.
The beauty of the root equals to number written on it.
输入格式
First line contains one integer number $n$ — the number of vertices in tree ( $1<=n<=2·10^{5}$ ).
Next line contains $n$ integer numbers $a_{i}$ ( $1<=i<=n$ , $1<=a_{i}<=2·10^{5}$ ).
Each of next $n-1$ lines contains two integer numbers $x$ and $y$ ( $1<=x,y<=n$ , $x≠y$ ), which means that there is an edge $(x,y)$ in the tree.
Next line contains $n$ integer numbers $a_{i}$ ( $1<=i<=n$ , $1<=a_{i}<=2·10^{5}$ ).
Each of next $n-1$ lines contains two integer numbers $x$ and $y$ ( $1<=x,y<=n$ , $x≠y$ ), which means that there is an edge $(x,y)$ in the tree.
输出格式
Output $n$ numbers separated by spaces, where $i$ -th number equals to maximum possible beauty of vertex $i$ .
输入输出样例
输入 #1
2 6 2 1 2
输出 #1
6 6
输入 #2
3 6 2 3 1 2 1 3
输出 #2
6 6 6
输入 #3
1 10
输出 #3
10
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted