Question C: there is a tree. At the beginning, each vertex has an initial value. The new number of each vertex is all the numbers in the path to the top of the tree. The maximum value of the maximum public factor after a number is removed,

Source: Internet
Author: User

Question C: there is a tree. At the beginning, each vertex has an initial value. The new number of each vertex is all the numbers in the path to the top of the tree. The maximum value of the maximum public factor after a number is removed,
Question

There is a tree. At the beginning, each vertex has an initial value. The new number of each vertex is all the numbers in the path to the top of the tree. The maximum value of the maximum public factor after a number is removed.

Practice

To remove a number, enumeration is obviously not displayed. Therefore, you can record the number of factors from the point to the root at this time in dfs, that is to say, each scan to a point breaks down the prime factor (remember to trace back). If the number of new factors reaches its depth-1, this number is available, find the largest one at the same time.

However, we also need to consider that it is not a new factor, that is, the number removed is the number of This vertex. At this time, the answer is the maximum public approx among all the numbers from the parent node to the root node, compare the two numbers to the maximum value.

Code
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 200100
using namespace std;

int n,m,dn[N],bb,first[N],g[N],deep[N],cnt[N],ans[N];
struct Bn
{
    int to,next;
}bn[N*2];

inline void add(int u,int v)
{
    bb++;
    bn[bb].to=v;
    bn[bb].next=first[u];
    first[u]=bb;
}

inline int fj(int u,int v)
{
    int i,res=1;
    cnt[u]++;
    if(cnt[u]>=v) res=u;
    for(i=2;i*i<=u;i++)
    {
        if(u%i==0)
        {
            cnt[i]++;
            if(cnt[i]>=v) res=max(res,i);
            if(i*i!=u)
            {
                cnt[u/i]++;
                if(cnt[u/i]>=v) res=max(res,u/i);
            }
        }
    }
    return res;
}

inline void ffj(int u)
{
    int i;
    cnt[u]--;
    for(i=2;i*i<=u;i++)
    {
        if(u%i==0)
        {
            cnt[i]--;
            if(i*i!=u)
            {
                cnt[u/i]--;
            }
        }
    }
}

inline int gcd(int u,int v)
{
    if(u<v) swap(u,v);
    for(;u!=v&&u&&v;swap(u,v))
    {
        u%=v;
    }
    return max(u,v);
}

void dfs(int now,int last)
{
    int p,q;
    for(p=first[now];p!=-1;p=bn[p].next)
    {
        if(bn[p].to==last) continue;
        deep[bn[p].to]=deep[now]+1;
        g[bn[p].to]=gcd(dn[bn[p].to],g[now]);

        ans[bn[p].to]=fj(dn[bn[p].to],deep[now]);
        ans[bn[p].to]=max(ans[bn[p].to],g[now]);
        dfs(bn[p].to,now);
        ffj(dn[bn[p].to]);
    }
}

int main()
{
    memset(first,-1,sizeof(first));
    register int i,j,p,q;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&dn[i]);
    }
    for(i=1;i<n;i++)
    {
        scanf("%d%d",&p,&q);
        add(p,q);
        add(q,p);
    }
    deep[1]=1;
    g[1]=ans[1]=dn[1];
    fj(dn[1],1);
    dfs(1,-1);
    for(i=1;i<=n;i++)
    {
        printf("%d ",ans[i]);
    }
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.