CF Round 430 C. Ilya And The Tree

题目链接http://codeforces.com/contest/842/problem/C

题目大意:给一棵树,树的每个节点有个非0整数。现在你要考察根到每个节点的路径上的所有元素的gcd,但是对于每个路径你都有一次机会将该路径上其中一个点变为0,路径之间没有关联。请输出根到每个节点的所求的值。

解题思路:暴力能过??? 

对于每个节点i,记录val[i] : 跟到节点i的每个节点值的gcd; set[i] : 根到节点i其中一个元素为0的所有元素的gcd的集合(不可重集)。

那么对于节点i的孩子j: val[j] = gcd(a[j], val[i]); set[j] = {gcd(set[i], a[j]), val[i])}。

代码:

 1 const int maxn = 4e5 + 5;
 2 struct edge{
 3     int to, next;
 4 };
 5 edge edges[maxn];
 6 int tot, head[maxn];
 7 int n, a[maxn], res[maxn];
 8 bool vis[maxn];
 9 set<int> ss[maxn / 2];
10 int val[maxn];
11 
12 void init(){
13     tot = 0;
14     memset(vis, 0, sizeof(vis));
15     memset(val, 0, sizeof(val));
16     memset(head, -1, sizeof(head));
17 }
18 void addEdge(int u, int v){
19     edges[tot].to = v;
20     edges[tot].next = head[u];
21     head[u] = tot++;
22 }
23 int gcd(int a, int b){
24     return b == 0? a: gcd(b, a % b);
25 }
26 void dfs(int x){
27     vis[x] = 1;
28     int tmans = val[x];
29     
30     for(int i = head[x]; i != -1; i = edges[i].next){
31         int v = edges[i].to;
32         if(vis[v]) continue;
33         val[v] = gcd(val[x], a[v]);
34         for(auto it: ss[x]){
35             int tmp = gcd(it, a[v]);
36             ss[v].insert(tmp);
37         }
38         ss[v].insert(val[x]);
39         dfs(v);
40     }
41     for(auto it: ss[x]) tmans = max(it, tmans);
42     res[x] = tmans;
43 }
44 void solve(){
45     val[1] = a[1]; ss[1].insert(0);
46     dfs(1);
47     for(int i = 1; i <= n; i++) {
48         printf("%d", res[i]);
49         if(i == n) puts("");
50         else printf(" ");
51     }
52 }
53 int main(){
54     init();
55     scanf("%d", &n);
56     for(int i = 1; i <= n; i++) scanf("%d", a + i);
57     for(int i = 1; i < n; i++){
58         int u, v;
59         scanf("%d %d", &u, &v);
60         addEdge(u, v);
61         addEdge(v, u);
62     }
63     solve();
64 }

题目:

C. Ilya And The Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ n1 ≤ ai ≤ 2·105).

Each of next n - 1 lines contains two integer numbers x and y (1 ≤ x, y ≤ nx ≠ 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 
posted @ 2017-09-02 10:47  EricJeffrey  阅读(711)  评论(0编辑  收藏  举报