1. 程式人生 > >C. Ilya And The Tree【樹形DP】

C. Ilya And The Tree【樹形DP】

C. Ilya And The Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

ExamplesinputCopy
2
6 2
1 2
output
6 6 
inputCopy
3
6 2 3
1 2
1 3
output
6 6 6 
inputCopy
1
10
output
10 

題意:給一張圖,每個節點有一個權值,一個節點的beauty值=gcd(root  to now's weight)   【允許在一條路徑上有一個數為0】,求出每個節點的最大beauty值

思路:對於當前的數x,其beauty值有兩種情況

①  把這個數設為0,前面所有值是原值

②這個數是原來的數,前面的值都是原值 || 父親滿足的情況

#include<bits/stdc++.h>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;

const int N=2e5+6;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;
vector <int> edge[N];
set<int> st[N];
int a[N],vis[N];

void dfs(int u,int fa,int gcd){

    st[u].insert(__gcd(gcd,a[u])); // ALL GCD
    st[u].insert(gcd); // let a[u]=0                                                            
    for(set<int>::iterator it=st[fa].begin();it!=st[fa].end();it++){
        int t=*it;
        st[u].insert(__gcd(t,a[u]));
    }
    gcd=__gcd(gcd,a[u]);
    vis[u]=1;
    for(int i=0;i<edge[u].size();++i){
        int v=edge[u][i];
        if(!vis[v]){
            dfs(v,u,gcd);
        }
    }
}

int main(void){
    int n;
    cin >> n;
    for(int i=1;i<=n;i++)   scanf("%d",&a[i]);
    for(int i=1;i<=n-1;i++){
        int u,v;
        scanf("%d%d",&u,&v);
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(1,0,0);
    for(int i=1;i<=n;i++){
        if(i==1)    printf("%d",*st[i].rbegin());
        else printf(" %d",*st[i].rbegin());
    }
    return 0;
}


相關推薦

C. Ilya And The Tree樹形DP

C. Ilya And The Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIlya is very fon

Codeforces1065F Up and Down the Tree 樹形DP

mes style clas dfs 可能 是個 con force void 推薦一道聯賽練習題。 題目分析: 你考慮進入一個子樹就可能上不來了,如果上得來的話就把能上來的全撿完然後走一個上不來的,所以這就是個基本的DP套路。 代碼: 1 #include<bi

Ilya And The Tree 樹上DFS

C. Ilya And The Tree time limit per test2 seconds memory limit per test256 megabytes Ilya is very fond of graphs, especially trees.

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

ret vector lis algo node main .com algorithm style http://codeforces.com/contest/842/problem/C 樹 dp 一個數的質因數有限,用set存儲,去重 1 #include

Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公約數+因子+樹)

C. Ilya And The Tree time limit per test 2 seconds memory limit per test 256 megabytes i

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

C. Ilya And The Tree time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output

Codeforces 842C Ilya And The Tree(樹上DP+因子個數估計)

C. Ilya And The Tree time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output

Codeforces 842C Ilya And The TreeDfs

C. Ilya And The Tree time limit per test 2 seconds memory limit per test 256 megabytes input

hdu5834 Magic boy Bi Luo with his excited tree 樹形dp

with ring code 實現 HA algorithm ase 表示 可能 題目鏈接 hdu5834 題解 思路很粗獷,實現很難受 設\(f[i][0|1]\)表示向子樹走回來或不回來的最大收益 設\(g[i][0|1]\)表示向父親走走回來或不回來的最大收益 再設\

CodeForces 161D Distance in Tree樹形DP

表示 ring sin 數量 can sizeof ans cst def <題目鏈接> 題目大意:一顆無向無環樹,有n個頂點,求其中距離為k的點對數是多少,(u,v)與(v,u)為同一點對。 1 #include <cstdio> 2

codeforces 842C Ilya And The Tree

直接 ber all 之前 tro numbers href cout graphs Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found

Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 樹形dp

題目連結: 傳送門 題意: 每個點有正權值ai,每條邊有負權值wi,你可以隨意選擇一條路徑,使得這條路徑的總權值最大,要求每個點每條邊至多都只能走一次。 思路: 一個頂點可以是路徑中的根節點或者是中間節點。 所以,設立兩個陣列,dpr,dpm. dpr表示為根節點的最

樹形dpGodfather

eal rtu 0ms rst root git 樹形 nbsp earch [POJ3107]Godfather Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 72

樹形dpTELE

lines ext 找到 mat orm money them spa number [POJ1155]TELE Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 537

樹形dpComputer

dig accept log names problem other vector http tchar Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav

樹形dpRebuilding Roads

sample tor http ber uil tran isdigit ast ext [POJ1947]Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Su

樹形dpTreasure Hunt I

int lin contain name tput pin who n-1 one day [ZOJ3626]Treasure Hunt ITime Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dange

BZOJ4446 [Scoi2015]小凸玩密室 樹形Dp

blog efi void 轉化 似的 聯通 while line down 題目 小凸和小方相約玩密室逃脫,這個密室是一棵有n個節點的完全二叉樹,每個節點有一個燈泡。點亮所有燈 泡即可逃出密室。每個燈泡有個權值Ai,每條邊也有個權值bi。點亮第1個燈泡不需要花費,之後每點

BZOJ4557 [JLoi2016]偵察守衛 樹形dp

str 相互 數據 max define display 初始 覆蓋 play 題目鏈接 BZOJ4557 題解 orz 比較難的樹形dp 不過想想也還好 看數據猜狀態,一維是點,一維是D 那麽就先設\(f[i][j]\)表示\(i\)所在子樹已處理完畢,還能向上【或向任意

POJ3585 Accumulation Degree 樹形dp

\n char rep sizeof def stream AI org gree 題目鏈接 POJ3585 題解 -二次掃描與換根法- 對於這樣一個無根樹的樹形dp 我們先任選一根進行一次樹形dp 然後再掃一遍通過計算得出每個點為根時的答案 #include<ios