Codeforces Round #430 (Div. 2) - C. Ilya And The Tree (树形DFS)

探讨树形结构中每个节点的最大可能beauty值计算问题,通过改变其中一个节点的值为0来最大化路径上的最大公约数(GCD),并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 



题意:

树形,每个节点有一个a[],求每个节点的beauty值:

把从根节点到这个节点经过的所有a[]收集起来,求他们的gcd。就是b值。

另外可以让某一个节点变为0,gcd(a,0)=a. gcd(0,a)=a;每次计算b值的时候都可以任意挑一个变为0,是独立的。


POINT:

利用set来保存当前节点的所有情况。 当前节点的情况可以由pre节点的情况全部推出来。


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
#define LL long long
const int maxn = 2e5+10;
int a[maxn];
set<int>s[maxn];
set<int>::iterator it;
vector<int>G[maxn];
int gcd(int x,int y)
{
    if(x<y) swap(x,y);
    return y==0?x:gcd(y,x%y);
}
void dfs(int u,int pre,int now)//now就是什么都不变的情况
{
    for(it=s[pre].begin();it!=s[pre].end();it++)
    {
        s[u].insert(gcd(*it,a[u]));//pre的情况加a[u]
    }
   // s[u].insert(now);//什么都不变的情况//这个不加的话,根节点的答案会错。
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(v==pre) continue;
        s[v].insert(now);//把v变成0的情况。
        dfs(v,u,gcd(now,a[v]));
    }
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d %d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    s[1].insert(0);//根变成0的情况
    s[1].insert(a[1]);
    dfs(1,0,a[1]);
    for(int i=1;i<=n;i++)
    {
        if(i-1) printf(" ");
        printf("%d",*s[i].rbegin());
    }
    printf("\n");
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值