zoukankan      html  css  js  c++  java
  • Codeforces Round #430 C. Ilya And The Tree

    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 ≤ 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.
    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

    题目大意:有一颗树,每一个节点有一个权值,每一个点的美丽度是该点到根节点1的所有点的权值的gcd,在求一个点时,可以去掉路径上任意一个点的权值(不计入gcd中),求每一个点的美丽度最大值

    解题报告:我不知道正解是什么,我的暴力水过了,大概思想如下,一个点不选只会影响到子树,然后我们就枚举删除每一个点,然后去更新其子树内的答案,设为f[i],这里明显可以剪枝,如果当前gcd==1,直接返回,记son[x]为x的子树内f的最小值,因为gcd满足递减,所以如果gcd<=son[x] 可以直接返回,然后就跑得过了

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    const int N=2e5+5;
    int n,num=0,head[N],to[N<<1],nxt[N<<1],a[N],f[N],son[N];
    void link(int x,int y){
    	nxt[++num]=head[x];to[num]=y;head[x]=num;
    }
    int gcd(int x,int y){
    	return x%y?gcd(y,x%y):y;
    }
    il void updata(int x,int last,int g){
    	int u;
    	if(g==1)return ;
    	if(g<=son[x])return ;
    	if(g>f[x])f[x]=g;son[x]=f[x];
    	for(RG int i=head[x];i;i=nxt[i]){
    		u=to[i];if(u==last)continue;
    		updata(u,x,gcd(g,a[u]));
    		son[x]=Min(son[x],son[u]);
    	}
    }
    il void dfs(int x,int last,int g){
    	int u;
    	if(g<=son[x])return ;
    	if(g>f[x])f[x]=g;
    	for(RG int i=head[x];i;i=nxt[i]){
    		u=to[i];if(u==last)continue;
    		updata(u,x,g);dfs(u,x,gcd(g,a[u]));
    	}
    }
    void work()
    {
    	int x,y;
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++)scanf("%d",&a[i]),f[i]=1;
    	for(int i=1;i<n;i++){
    		scanf("%d%d",&x,&y);
    		link(x,y);link(y,x);
     	}
    	dfs(1,1,a[1]);
    	for(int i=head[1];i;i=nxt[i]){
    		updata(to[i],1,a[to[i]]);
    	}
    	for(int i=1;i<=n;i++)printf("%d ",f[i]);
    }
    
    int main()
    {
    	work();
    	return 0;
    }
    
  • 相关阅读:
    ios-点击图片放大,背景变半透明
    为代码分段标识
    方法的标签_With携带
    使用json要导入什么包
    Json中不支持任何形式的注释,那我们要怎么解决呢
    JFinal中文件上传后会默认放置到WebContent的upload包下,但是tomcat会自动重启,当我们再次打开upload文件夹查看我们刚刚上传的文件时,发现上传的文件已经没有了。
    JFinal上传文件时用getFile()方法报错
    JFinal文件上传时直接使用getPara()去接受表单的数据接收到的数据一直是null?
    Freemarker全部文档和具体实例
    Eclipse安装Freemarker插件
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7451552.html
Copyright © 2011-2022 走看看