地址:http://codeforces.com/contest/842/problem/C

题目:

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 ≤ n, 1 ≤ 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 

思路:

  用set<int>dp[K]来表示走到节点i时前面修改一个数时的所有可能值,然后转移即可。

  看起来是n^2的dp,但约数个数是非常少的。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int v[K];
vector<int>mp[K];
set<int>dp[K]; void dfs(int x,int f,int sum)
{
for(auto &y:dp[f])
dp[x].insert(__gcd(v[x],y));
dp[x].insert(__gcd(sum,v[x]));
dp[x].insert(__gcd(,sum));
for(auto &y:mp[x])
if(y!=f)
dfs(y,x,__gcd(v[x],sum));
}
int main(void)
{
int n;
cin>>n;
for(int i=;i<=n;i++)
scanf("%d",v+i);
for(int i=,x,y;i<n;i++)
scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
dfs(,,);
for(int i=;i<=n;i++)
printf("%d ",*dp[i].rbegin());
return ;
}

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

  1. Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心

    Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  2. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  3. 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

    题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...

  4. 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.直接暴力判断即 ...

  5. Codeforces Round #430 (Div. 2)

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

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

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

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

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

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

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

  9. Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题

    A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...

随机推荐

  1. CSS顶级技巧大放送,div+css布局必知

    字体大小使用px 在一行内声明CSS 对比下面两个: h2 {font-size:18px; border:1px solid blue; color:#000; } h2 {    font-siz ...

  2. JavaScript 二、eval 和 with 函数

    /* * ========================================================= * * JavaScript 词法欺骗 * * 1.欺骗词法作用域,会导致 ...

  3. Spring_day02--Spring的aop操作

    Spring的aop操作 1 在spring里面进行aop操作,使用aspectj实现 (1)aspectj不是spring一部分,和spring一起使用进行aop操作 (2)Spring2.0以后新 ...

  4. 第五篇:CUDA 并行程序中的同步

    前言 在并发,多线程环境下,同步是一个很重要的环节.同步即是指进程/线程之间的执行顺序约定. 本文将介绍如何通过共享内存机制实现块内多线程之间的同步. 至于块之间的同步,需要使用到 global me ...

  5. PHP mysql基本语句指令

    /*选择数据库 use test; */ /* 显示所有的数据库 show databases; */ /*删除表/数据库 drop database test1; delete from user1 ...

  6. JavaScript 禁止表单提交

    有时我们需要在表单真正提交之前,做一些检查工作,检查通过之后再进行提交. <form name="myForm" onsubmit = "validateMyFor ...

  7. 【转】dd命令详解及利用dd测试磁盘性能

    dd命令详解及利用dd测试磁盘性能   linux下dd命令详解 名称: dd 使用权限: 所有使用者 manpage 定义: convert and copy a file 使用方式: dd [op ...

  8. JUnit4 测试示例

    1. JUnit4 测试示例 // Calculator.java public class Calculator{ public int add(int a, int b){ return a + ...

  9. mybatis拓展框架对比

    对比项 mybatis-plus mybatis-enhance mapper fastmybatis 表名映射 @TableName @Table JPA:@Table JPA:@Table 主键映 ...

  10. python collections模块 计数器(counter)

    一.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 把我写入的元素出现的多少次都计算出来 import collectio ...