Codeforces Round #430 (Div. 2) C. Ilya And The Tree
地址:http://codeforces.com/contest/842/problem/C
题目:
2 seconds
256 megabytes
standard input
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.
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 n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
2
6 2
1 2
6 6
3
6 2 3
1 2
1 3
6 6 6
1
10
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的更多相关文章
- 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 ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...
- 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.直接暴力判断即 ...
- Codeforces Round #430 (Div. 2)
A. Kirill And The Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 【Codeforces Round #430 (Div. 2) A C D三个题】
·不论难度,A,C,D自己都有收获! [A. Kirill And The Game] ·全是英文题,述大意: 给出两组区间端点:l,r,x,y和一个k.(都是正整数,保证区间不为空),询问是否 ...
- C - Ilya And The Tree Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/C 树 dp 一个数的质因数有限,用set存储,去重 #include <cstdio> #includ ...
- 【Codeforces Round #430 (Div. 2) C】Ilya And The Tree
[链接]点击打开链接 [题意] 给你一棵n个点的树,每个点的美丽值定义为根节点到这个点的路径上的所有权值的gcd. 现在,假设对于每一个点,在计算美丽值的时候,你可以将某一个点的权值置为0的话. 问你 ...
- 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/ ...
随机推荐
- 用css制作星级评分
Step 1: XHTML <ul class="star-rating"> <li><a href="#" titl ...
- Mac 安装Jupyter notebook
python:mac下自带Python 2.7.10 1.先升级了pip安装工具:sudo python -m pip install --upgrade --force pip 2.安装setupt ...
- MathType中如何快速输入空心字母
MathType输入数学公式时非常方便有效的,在文档中涉及到数学公式或者符号时都是使用MathType来进行的,因为它比Office自带的公式编辑器功能更为完善,使用起来更快捷.在数学公式中,在不同的 ...
- hdu 4291(矩阵+暴力求循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4291 思路:首先保留求出循环节,然后就是矩阵求幂了. #include<iostream> ...
- iOS开发之-- 设置启动图片
一.添加启动图片 点击Assets.xcassets进入图片管理,右击,弹出"New Launch Image"或点下面的+号创建Launch Image: 如图,右侧的勾选可以让 ...
- HTML5游戏制作完全指南
简介 创建画布 游戏循环 Hello world 创建player 键盘控制 a:使用jQuery Hotkeys b:移动player 添加更多游戏元素 炮弹 敌人 使用图片 碰撞检测 声音 简介 ...
- 【黑金原创教程】 FPGA那些事儿《概念篇》
简介一本讲述非软硬片上系统的书,另外还是低级建模的使用手册. 目录[黑金原创教程] FPGA那些事儿<概念篇>:File01 - 结构的玩笑[黑金原创教程] FPGA那些事儿<概念篇 ...
- android-修改TextView中部分文字的颜色
:
- html的table列表根据奇数还是偶数行显示不同颜色
<tr <s:if test="#sts.even"> class="table_1" onMouseOut="this.class ...
- 理解Javascript__理解undefined和null
来自普遍的回答: 其实在 ECMAScript 的原始类型中,是有Undefined 和 Null 类型的. 这两种类型都分别对应了属于自己的唯一专用值,即undefined 和 null. 值 un ...