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.

给出一棵生成树,每个节点都有一个值,现在要求出每个节点的美丽值的最大值,美丽值的定义为从根节点到该节点(包含)路径上所有点的值的gcd,求解每个 点时可以把路径上某一个点的值变为0(就相当于删除这个节点的数)。你可以认为每个点美丽值的求解是独立的(每次删除的点都不会影响下一次)。

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 

我们用c[i]表示从1~i不变化的gcd值

用set[i]表示已变化的gcd的值的集合

对于u->v有

set[v].insert(c[u])表示将v变化

set[v][]=gcd(set[u][i],a[v])在v之前已变化

但这样可能存在疑问,每一次的集合都增加一个,而且还要遍历

这岂不是n^2???

但set去重后数量却远远达不到n,也就√n

因为a[x]的因数总共就不超过√a[x]个,不可能超过这么多,而a[x]<=20^5

所以可以过

输出直接输出集合中最大的数

代码这里用了STL中的集合

%%%%yzh巨佬用n^2暴力强行过:

http://www.cnblogs.com/Yuzao/p/7451552.html

果然巨佬还是强无敌啊

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
struct Node
{
int next,to;
}edge[];
int num,head[],c[],a[],n;
set<int>Set[];
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
int gcd(int a,int b)
{
if (!b) return a;
return gcd(b,a%b);
}
void dfs(int x,int pa)
{
for (int i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
c[v]=gcd(c[x],a[v]);
Set[v].insert(c[x]);
for (set<int>::iterator i=Set[x].begin();i!=Set[x].end();i++)
Set[v].insert(gcd(a[v],*i));
dfs(v,x);
}
}
int main()
{int i,u,v;
cin>>n;
for (i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for (i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
c[]=a[];
Set[].insert();
dfs(,);
cout<<a[]<<' ';
for (i=;i<n;i++)
printf("%d ",*(--Set[i].end()));
if (n>)
cout<<*(--Set[n].end())<<endl;
}

最新文章

  1. java异常处理(父子异常的处理)
  2. Win7上的ASP.NET MVC3项目在Win10上运行的一个坑
  3. JQuery插件Validation的使用-遁地龙卷风
  4. UVA 156 Ananagrams ---map
  5. Linux基础与Linux下C语言编程基础
  6. ArcGIS API for Silverlight代码中使用Template模板
  7. 【莫比乌斯反演】关于Mobius反演与gcd的一些关系与问题简化(bzoj 2301 Problem b&amp;&amp;bzoj 2820 YY的GCD&amp;&amp;BZOJ 3529 数表)
  8. 简单的monkey使用
  9. Business Intelligence (BI)
  10. wpf 透明效果 需要DwmApi.dll文件,然后定义一个函数去画Aero区域,从而实现整个窗口的Aero化。
  11. PAT (Advanced Level) 1049. Counting Ones (30)
  12. Python爬虫 股票数据爬取
  13. 责任链模式-Chain of Responsibility(Java实现), 例2
  14. DB2 因版本问题 Reorg 出错 解决办法
  15. vim 常用 NERDTree 快捷键
  16. Cordova结合Vue学习Camera
  17. C. Vasya and Multisets
  18. Windows平台下面Oracle11.2.0.1 升级Oracle11.2.0.4 的简单步骤
  19. [No0000D4]批处理全部代码详解Allbat
  20. jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第一话):初次启动jenkins,输入给定密码后登录失败问题解决

热门文章

  1. JavaScript(第十三天)【内置对象】
  2. 解决fiddler无法抓取本地部署项目的请求问题
  3. Alpha阶段报告-hywteam
  4. lambda及参数绑定
  5. Swift - 使用导航条和导航条控制器来进行页面切换并传递数据
  6. python 一篇搞定所有的异常处理
  7. Hangfire使用ApplicationInsigts监控
  8. spring5——Aop的实现原理(动态代理)
  9. Mac 中配置Apache
  10. PHP7链接MySQL