Codeforces 842 C. Ilya And The Tree (dfs)
来源:互联网 发布:淘宝客服接待用语 编辑:程序博客网 时间:2024/06/06 01:26
Description
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·10^5).
Next line contains n integer numbers ai (1 ≤ i ≤ n, 1 ≤ ai ≤ 2·10^5).
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
Output n numbers separated by spaces, where i-th number equals to maximum possible beauty of vertex i.
Sample Input
36 2 31 21 3
Sample Output
6 6 6
题意
一棵根节点为 1 的树,每个节点都有权值,求从根节点到每个点路径所有权值的最大 gcd 。(我们可以把每条路径上一个点的权值变为 0 )
思路
显然,我们可以通过 dfs 来求解这个问题。
对于每一个点 x ,其结果是一个集合。
- 我们可以不选择 x (权值变为 0 ),此时到这里的贡献为从根节点到当前位置除 x 以外所有数的 gcd ;
- 我们也可以选择 x ,此时的贡献有:
- 从根节点到当前位置所有数的 gcd
- 当前数与其父节点所有符合条件的值计算 gcd
然后对于每一个数所拥有的一个集合,取最大值输出即可。
AC 代码
#include <bits/stdc++.h>using namespace std;const int maxn=2e5+10;int a[maxn],n;vector<int>g[maxn];set<int>s[maxn];void dfs(int x,int fa,int now){ for(auto p:s[fa]) s[x].insert(__gcd(p,a[x])); s[x].insert(now); //当前点置零 now=__gcd(now,a[x]); s[x].insert(now); //当前点保留 for(auto p:g[x]) if(p!=fa) dfs(p,x,now);}int main(){ scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",a+i); int u,v; for(int i=1; i<n; i++) { scanf("%d%d",&u,&v); g[u].push_back(v); g[v].push_back(u); } dfs(1,0,0); for(int i=1; i<=n; i++) printf("%d ",*s[i].rbegin()); return 0;}
- Codeforces 842 C. Ilya And The Tree (dfs)
- Codeforces 842 C. Ilya And The Tree (dfs)
- Codeforces 842C Ilya And The Tree【Dfs】
- codeforces 842C. Ilya And The Tree(dfs)
- Codeforces 842C Ilya And The Tree
- Codeforces 842 C Ilya And The Tree
- Codeforces Round #430(Div.2)Problem C Ilya And The Tree(DFS)
- Codeforces 842 C. Ilya And The Tree 树上dp
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)
- Codeforces Round #430 (Div. 2)C. Ilya And The Tree dfs D
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces 842C Ilya And The Tree(树上DP+因子个数估计)
- 842C Ilya And The Tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- Codeforces 430 Div 2 C.Ilya And The tree
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
- codeforce 842C. Ilya And The Tree(dfs用set存储到每种可能)
- Crop人脸图
- 对Unity3D中对象池的尝试
- The server does not support version 3.0 of the J2EE Web module specification
- Java虚拟机类加载机制
- Oracle 11.2.0_Phy-DataGuard_CentOS7.2
- Codeforces 842 C. Ilya And The Tree (dfs)
- 面对国产闪存疯狂追击,三星扩大中国NAND产能
- java编程思想之一切都是对象
- 网易内推(网易金融)前端面试
- 项目中遇到的问题
- ios 基础知识点总结
- 更改Ubuntu默认Python版本方法
- CUDA 关于 BLOCK数目与Thread数目设置
- C# 能否获取一个对象所占内存的大小?