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


    地址:http://codeforces.com/contest/842/problem/C

    题目:

    C. Ilya And The Tree
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

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

     思路:

      用set<int>dp[K]来表示走到节点i时前面修改一个数时的所有可能值,然后转移即可。

      看起来是n^2的dp,但约数个数是非常少的。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int v[K];
    15 vector<int>mp[K];
    16 set<int>dp[K];
    17 
    18 void dfs(int x,int f,int sum)
    19 {
    20     for(auto &y:dp[f])
    21         dp[x].insert(__gcd(v[x],y));
    22     dp[x].insert(__gcd(sum,v[x]));
    23     dp[x].insert(__gcd(0,sum));
    24     for(auto &y:mp[x])
    25     if(y!=f)
    26         dfs(y,x,__gcd(v[x],sum));
    27 }
    28 int main(void)
    29 {
    30     int n;
    31     cin>>n;
    32     for(int i=1;i<=n;i++)
    33         scanf("%d",v+i);
    34     for(int i=1,x,y;i<n;i++)
    35         scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
    36     dfs(1,0,0);
    37     for(int i=1;i<=n;i++)
    38         printf("%d ",*dp[i].rbegin());
    39     return 0;
    40 }
  • 相关阅读:
    Jmeter参数化-用户定义的变量
    Jmeter进行文件下载
    Jmeter进行文件上传
    Jmeter进行HTTP接口测试
    Jmeter元件作用域及执行顺序
    activiti 汉化
    Spring boot web app项目
    spring boot整合activiti rest api详细教程
    Spring Boot自动配置原理
    spring bean注解使用详解
  • 原文地址:https://www.cnblogs.com/weeping/p/7452418.html
Copyright © 2020-2023  润新知