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

题目链接

C. Ilya And The Tree

time limit per test 2 seconds

memory limit per test 256 megabytes

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 

题目描述:

一棵根节点为 x的树,每个节点i都有权值a[x],求从根节点到每个点路径的所有权值的 gcd最大值(可以把每条路径上任意一个点的权值变为 0 )。

Solution:

DFS遍历每个节点,用set(避免重复值)存储gcd

对于每一个点 x,将其所有可能的gcd结果存入集合s[x]。

  • 如果不选择 x (权值变为 0 ),此时到这里的贡献为从根节点到当前位置的父节点所有数的 gcd;
  • 如果选择 x ,此时的贡献有: 
    • 从根节点到当前位置所有节点的权值的 gcd
    • 当前权值与其父节点所有可能的 gcd值(即s[fa],fa为当前 x节点的父节点)计算 gcd,可以理解为是从根节点到当前节点的父节点中将任意一个节点的权值变为了0。
  • 然后对于每一个节点所拥有的集合s[x],取最大值输出。

迭代器:

c.begin() 返回一个迭代器,它指向容器c的第一个元素

c.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置

c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素

c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置

int main()
{
	set<int> s;
	s.insert(9), s.insert(8), s.insert(7);
	set<int>::iterator it;
	for(it = s.begin(); it != s.end(); it++) //set的遍历
		printf("%d ", *it); //输出结果:7 8 9
	printf("%d\n", *s.begin()); //输出结果:7
	printf("%d\n", *s.end());   //输出结果:3
	printf("%d\n", *s.rbegin()); //输出结果:9
	printf("%d\n", *s.rend()); //输出结果:3
	return 0;
}

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#define mst(a, b) memset(a, b, sizeof(a))
#define rush() int T; scanf("%d", &T); while(T--)
using namespace std;
typedef pair<int, int> PII;
const int MaxN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

//priority_queue<int, vector<int>, greater<int> > que;
vector<int> G[MaxN];
set<int> s[MaxN];
int a[MaxN], vis[MaxN];
int n;

int gcd(int a, int b) {
	if(b == 0) return a;
	return gcd(b, a % b);
}
//x表示当前节点,fa表示当前节点的父亲节点,now表示从根节点到当前节点的所有节点权值的gcd(在刚开始传入DFS时的now为从根节点到当前节点的父节点的所有节点权值的gcd)
void DFS(int x, int fa, int now) {
	set<int>::iterator it;
	for(it = s[fa].begin(); it != s[fa].end(); it++) 
		s[x].insert(gcd(a[x], *it)); //当前权值与其父节点所有可能的gcd值计算gcd(即从根节点到当前节点的父节点中将任意一个节点的权值变为了0)
	s[x].insert(now); //将当前x节点置0
	now = gcd(now, a[x]); //从根节点到当前节点所有节点的gcd,不选择置0
	s[x].insert(now);
	for(int i = 0; i < G[x].size(); i++) {
		int v = G[x][i];
		if(vis[v] || v == fa) continue;
		vis[v] = 1;
		DFS(v, x, now); //继续向下遍历
	}
}

int main()
{
	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);
	}
	DFS(1, 0, 0);
	for(int i = 1; i <= n; i++) {
		printf("%d", *s[i].rbegin());
		i == n ? printf("\n") : printf(" ");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jasmineaha/article/details/81108296
今日推荐