1. 程式人生 > >C - Ilya And The Tree Codeforces Round #430 (Div. 2)

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 <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <time.h>
  6 #include <string>
  7 #include <set>
  8 #include <map>
  9 #include <list>
 10
#include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22
#define E 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=2e5+10; 25 26 struct node 27 { 28 int d; 29 node* next; 30 }*e[maxn]; 31 32 set<int>y[maxn]; //delete one 33 int a[maxn],x[maxn],value[maxn]; 34 bool vis[maxn]={0}; 35 36 void dfs(int d) 37 { 38 set
<int>::iterator i; 39 int dd; 40 41 value[d]=x[d]; 42 for (i=y[d].begin();i!=y[d].end();i++) 43 value[d]=max(value[d],*i); 44 45 node* p=e[d]; 46 vis[d]=1; 47 while (p) 48 { 49 dd=p->d; 50 if (!vis[dd]) 51 { 52 x[dd]=__gcd(x[d],a[dd]); 53 y[dd].insert(x[d]); 54 for (i=y[d].begin();i!=y[d].end();i++) 55 y[dd].insert(__gcd(a[dd],*i)); 56 if (d==1) 57 y[dd].insert(a[dd]); 58 dfs(dd); 59 } 60 p=p->next; 61 } 62 //free memory 63 y[d].clear(); 64 } 65 66 int main() 67 { 68 node* p; 69 int n,X,y,i; 70 scanf("%d",&n); 71 for (i=1;i<=n;i++) 72 scanf("%d",&a[i]); 73 for (i=1;i<n;i++) 74 { 75 scanf("%d%d",&X,&y); 76 p=(node*) malloc (sizeof(node)); 77 p->d=y; 78 p->next=e[X]; 79 e[X]=p; 80 81 p=(node*) malloc (sizeof(node)); 82 p->d=X; 83 p->next=e[y]; 84 e[y]=p; 85 } 86 87 x[1]=a[1]; 88 dfs(1); 89 90 for (i=1;i<n;i++) 91 printf("%d ",value[i]); 92 printf("%d",value[i]); 93 return 0; 94 } 95 /* 96 3 97 6 10 12 98 1 2 99 2 3 100 */

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