codeforces 842C Ilya And The Tree

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

果然巨佬还是强无敌啊

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<set>
 6 using namespace std;
 7 struct Node
 8 {
 9   int next,to;
10 }edge[400001];
11 int num,head[200001],c[200001],a[200001],n;
12 set<int>Set[200001];
13 void add(int u,int v)
14 {
15   num++;
16   edge[num].next=head[u];
17   head[u]=num;
18   edge[num].to=v;
19 }
20 int gcd(int a,int b)
21 {
22   if (!b) return a;
23   return gcd(b,a%b);
24 }
25 void dfs(int x,int pa)
26 {
27   for (int i=head[x];i;i=edge[i].next)
28     {
29       int v=edge[i].to;
30       if (v==pa) continue;
31       c[v]=gcd(c[x],a[v]);
32       Set[v].insert(c[x]);
33       for (set<int>::iterator i=Set[x].begin();i!=Set[x].end();i++)
34     Set[v].insert(gcd(a[v],*i));
35       dfs(v,x);
36     }
37 }
38 int main()
39 {int i,u,v;
40   cin>>n;
41   for (i=1;i<=n;i++)
42     {
43       scanf("%d",&a[i]);
44     }
45   for (i=1;i<=n-1;i++)
46     {
47       scanf("%d%d",&u,&v);
48       add(u,v);add(v,u);
49     }
50   c[1]=a[1];
51   Set[1].insert(0);
52   dfs(1,0);
53   cout<<a[1]<<' ';
54   for (i=2;i<n;i++)
55     printf("%d ",*(--Set[i].end()));
56   if (n>1)
57   cout<<*(--Set[n].end())<<endl;
58 }

 

转载于:https://www.cnblogs.com/Y-E-T-I/p/7684146.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值