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


    【链接】点击打开链接


    【题意】


    给你一棵n个点的树,每个点的美丽值定义为根节点到这个点的路径上的所有权值的gcd.
    现在,假设对于每一个点,在计算美丽值的时候,你可以将某一个点的权值置为0的话.
    问你每个点的最大美丽值可能是多少.

    【题解】


    从根节点开始进行dfs,在往下走的过程中,暴力用set记录下路径中把以上的每一个点删掉后的gcd是什么.
    set里的东西.进行扩展的时候,只能加入不删的点.因为它已经表示删掉一个点的状态了.下面只能都不删
    .然后用另外一个数组now,记录下到根节点到当前这个点的路径上的所有权值的gcd为多少.这样方便处理
    某个节点删掉之后的gcd是什么.
    然后用set里的东西的最大值和now[y]比较,取较大值作为y节点的答案就可以了。


    【错的次数】


    0

    【反思】


    完全没想到可以这么用。。
    一直在想怎么快速获取删掉之后的一个值。
    一直在想是不是gcd有什么性质.
    然后也没有想到一个类似前缀和的东西可以快速维护删掉某个点之后,剩下的数字的gcd.
    下次思考问题的时候,直接一点。不要老是想一些歪门邪道的东西!

    【代码】

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <set>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb emplace_back
    #define fi first
    #define se second
    #define ld long double
    #define ms(x,y) memset(x,y,sizeof x)
    #define ri(x) scanf("%d",&x)
    #define rl(x) scanf("%lld",&x)
    #define rs(x) scanf("%s",x)
    #define rf(x) scnaf("%lf",&x)
    #define oi(x) printf("%d",x)
    #define ol(x) printf("%lld",x)
    #define oc putchar(' ')
    #define os(x) printf(x)
    #define all(x) x.begin(),x.end()
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0)
    #define sz(x) ((int) x.size())
    #define ld long double
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //mt19937 myrand(time(0));
    //int get_rand(int n){return myrand()%n + 1;}
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 2e5;
    
    int n,a[N+10],now[N+10],ans[N+10];
    vector <int> g[N+10];
    
    void dfs(int x,int fa,set <int> myset){
        int len = sz(g[x]);
        rep1(i,0,len-1){
            int y = g[x][i];
            if (y==fa) continue;
            now[y] = __gcd(now[x],a[y]);
            set <int> tset;
            set <int>::iterator it;
            for (it = myset.begin();it!=myset.end();it++){
                int temp = *it;
                tset.insert(__gcd(temp,a[y]));
            }
            tset.insert(now[x]);
            ans[y] = max(now[y],*(--tset.end()));
            dfs(y,x,tset);
        }
    }
    
    int main(){
        //Open();
        //Close();
        ri(n);
        rep1(i,1,n) ri(a[i]);
        rep1(i,1,n-1){
            int x,y;
            ri(x),ri(y);
            g[x].pb(y),g[y].pb(x);
        }
        ans[1] = now[1] = a[1];
        dfs(1,0,{0});
        rep1(i,1,n)
            oi(ans[i]),oc;
        return 0;
    }


  • 相关阅读:
    react获取ref的几种形式
    vue与react的小区别
    vue当中计算属性重新计算依赖关系
    移动端常见问题
    WX小程序--开发中相关问题记录
    ECMAScript6 入门 Set 和Map结构
    ECMAScript6 入门 Class 的基本语法与继承
    ECMAScript6 入门 Generator
    ECMAScript6 入门 Promise
    ECMAScript6 入门 Symbol
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626070.html
Copyright © 2020-2023  润新知