题意:求根节点到所有叶子节点,所有路径上的点的最大公约数,可以让一个点为0,暴力搜索,set去重
#include<bits\stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
vector <int>g[200005];
set<int>s[200005];
int a[200005];
void dfs(int x,int fa,int now)
{
for(auto tmp:s[fa])
s[x].insert(__gcd(tmp,a[x]));
s[x].insert(now);
now=__gcd(now,a[x]);
s[x].insert(now);
// cout<<x<<endl;
// getchar();
for(auto tmp:g[x])
{
if(tmp!=fa)
dfs(tmp,x,now);
}
}
int main(){
int n;
while(~scanf("%d",&n))
{
int x,y;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<n;i++)
{scanf("%d%d",&x,&y);g[x].pb(y);g[y].pb(x);}
dfs(1,0,0);
for(int i=1;i<=n;i++)
printf("%d ",*s[i].rbegin());
}
return 0;
}