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/ ...
随机推荐
- asp.net 动态添加多个用户控件
动态添加多个相同用户控件,并使每个用户控件获取不同的内容. 用户控件代码: 代码WebControls using System; using System.Collections.Generic; ...
- 【转】关于OnPaint的工作机制
转载出处:http://blog.csdn.net/foreverhuylee/article/details/21889025 用了两年的VC++,其实对OnPaint的工作原理一直都是一知半解.这 ...
- mac下MAMP的安装和使用
详情博客:https://my.oschina.net/laiconglin/blog/514139
- 《C++ Primer Plus》学习笔记 2.1.3 C++预处理器和iostream文件
程序清单2-1 myfirst.cpp // myfirst.cpp -- displays a message #include <iostream> // a PREPROCESSOR ...
- Zabbix-3.0.3实现微信(WeChat)告警
导读 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告 ...
- [Android] 开源框架 xUtils HttpUtils 代理设置 (Temporary Redirect错误)
今天简单学习了一下xUtils的使用 https://github.com/wyouflf/xUtils 其中用到HttpUtils模块时,发现总是出现Temporary Redirect 错误. 查 ...
- CH5101 LCIS【线性dp】
5101 LCIS 0x50「动态规划」例题 描述 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们研究最长公共上升子序列了 ...
- c# 下三角实现 九九乘法口诀表
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Hell ...
- Redis集群部署文档(Ubuntu15.10系统)
Redis集群部署文档(Ubuntu15.10系统)(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如 ...
- 使用Nana进行C++ GUI开发
Nana官网地址:nanapro.org 简单示例:NanaDemo.cpp #include <nana/gui.hpp> #include <nana/gui/widgets/b ...