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 ai.

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.

Input

First line contains one integer number n — the number of vertices in tree (1 ≤ n ≤ 2·105).

Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·105).

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

Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.

Examples

Input

2

6 2

1 2

Output

6 6

Input

3

6 2 3

1 2

1 3

Output

6 6 6

Input

1

10

Output

10

题目大意:有一颗树,每一个节点有一个权值,每一个点的美丽度是该点到根节点1的所有点的权值的gcd,在求一个点时,可以去掉路径上任意一个点的权值(不计入gcd中),求每一个点的美丽度最大值

解题报告:我不知道正解是什么,我的暴力水过了,大概思想如下,一个点不选只会影响到子树,然后我们就枚举删除每一个点,然后去更新其子树内的答案,设为f[i],这里明显可以剪枝,如果当前gcd==1,直接返回,记son[x]为x的子树内f的最小值,因为gcd满足递减,所以如果gcd<=son[x] 可以直接返回,然后就跑得过了

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=2e5+5;
int n,num=0,head[N],to[N<<1],nxt[N<<1],a[N],f[N],son[N];
void link(int x,int y){
nxt[++num]=head[x];to[num]=y;head[x]=num;
}
int gcd(int x,int y){
return x%y?gcd(y,x%y):y;
}
il void updata(int x,int last,int g){
int u;
if(g==1)return ;
if(g<=son[x])return ;
if(g>f[x])f[x]=g;son[x]=f[x];
for(RG int i=head[x];i;i=nxt[i]){
u=to[i];if(u==last)continue;
updata(u,x,gcd(g,a[u]));
son[x]=Min(son[x],son[u]);
}
}
il void dfs(int x,int last,int g){
int u;
if(g<=son[x])return ;
if(g>f[x])f[x]=g;
for(RG int i=head[x];i;i=nxt[i]){
u=to[i];if(u==last)continue;
updata(u,x,g);dfs(u,x,gcd(g,a[u]));
}
}
void work()
{
int x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),f[i]=1;
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
link(x,y);link(y,x);
}
dfs(1,1,a[1]);
for(int i=head[1];i;i=nxt[i]){
updata(to[i],1,a[to[i]]);
}
for(int i=1;i<=n;i++)printf("%d ",f[i]);
} int main()
{
work();
return 0;
}

Codeforces Round #430 C. Ilya And The Tree的更多相关文章

  1. Codeforces Round #430 (Div. 2) C. Ilya And The Tree

    地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...

  2. Codeforces Round #430 (Div. 2) 【A、B、C、D题】

    [感谢牛老板对D题的指点OTZ] codeforces 842 A. Kirill And The Game[暴力] 给定a的范围[l,r],b的范围[x,y],问是否存在a/b等于k.直接暴力判断即 ...

  3. Codeforces Round #430 (Div. 2)

    A. Kirill And The Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. 【Codeforces Round #430 (Div. 2) A C D三个题】

    ·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意:    给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...

  5. C - Ilya And The Tree Codeforces Round #430 (Div. 2)

    http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...

  6. 【Codeforces Round #430 (Div. 2) C】Ilya And The Tree

    [链接]点击打开链接 [题意] 给你一棵n个点的树,每个点的美丽值定义为根节点到这个点的路径上的所有权值的gcd. 现在,假设对于每一个点,在计算美丽值的时候,你可以将某一个点的权值置为0的话. 问你 ...

  7. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  8. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

  9. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

随机推荐

  1. Beta No.3

    今天遇到的困难: 组员对github极度的不适应 github的版本控制和协同化编程确实操作起来需要一定的熟练度,我们缺乏这种熟练度 Android Studio版本不一致项目难以打开的问题仍然无法解 ...

  2. 2017-2018-1 Java演绎法 第四五周 作业

    团队任务:撰写<需求规格说明书> 团队组长:袁逸灏 本次编辑:刘伟康 流程.分工.比例 (比例按照任务的费时.难度和完成情况估算) 流程 确定任务 -→ 分配任务 -→ 各组员完成各自任务 ...

  3. mongodb 复制(副本集)

    复制(副本集) 什么是复制 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性,并可以保证数据的安全性 复制还允许从硬件故障和服务中断中恢复数据 为什么要复制 数据备份 数据灾 ...

  4. verilog学习笔记(4)_有限状态机

    有限状态机: 有限状态机是由寄存器组和组合逻辑构成的硬件时序电路: - 其状态(即由寄存器组的1和0的组合状态所构成的有限个状态)只能在同一时钟跳变沿的情况下才能从一个状态转向另一个状态: - 究竟转 ...

  5. nyoj 孪生素数

    孪生素数问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 写一个程序,找出给出素数范围内的所有孪生素数的组数.一般来说,孪生素数就是指两个素数距离为2,近的不能再 ...

  6. Apache自带 ab压测工具 Windows配置使用说明 - 随笔记录

    我们先来了解一下ab工具的概念,摘自网络: ab是apache自带的压力测试工具.ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试.比如ngin ...

  7. LeetCode & Q20-Valid Parentheses-Easy

    Stack String Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ...

  8. linux下的Shell编程(3)shell里的流程控制

    if 语句 if 表达式如果条件命令组为真,则执行 then 后的部分.标准形式: if 判断命令,可以有很多个,真假取最后的返回值 then 如果前述为真做什么 [ # 方括号代表可选,别真打进去了 ...

  9. IDE-Android Studio -FAQ-使用习惯(不断更新 欢迎留言)

    摘要: 从ecplise工具切换到android studio后遇到了很多问题,起初亦非常痛苦,城墙内外阅博无数才得以解决.所以把当时遇到的问题记录下来,方便后来人学习. 另如果有遇到未纪录的问题欢迎 ...

  10. jQuery serialize()方法获取不到数据,alert结果为空

    网上查找,问题可能是 id有重复 经排查,没有发现重复id 解决方案 form表单中每个input框都没有name属性,添加name属性即可 若name属性与jQuery的关键字有冲突,也可导致该问题 ...