zoukankan      html  css  js  c++  java
  • 【TYVJ 1052】没有上司的舞会

    【原题链接】传送门

    【调试出错】

    我代码里的for循环一般用define  rep

    这里循环(vector)son的下标时(0,son[x].size()-1)出错了

    问题未知。

    【code】

    #include<bits/stdc++.h>
    using namespace std;
    #define File ""
    #define ll long long
    #define ull unsigned long long
    #define rep(k,i,j) for(int k = i;k <= j; ++k)
    #define FOR(k,i,j) for(int k = i;k >= j; --k)
    inline void file(){
        freopen(File".in","r",stdin);
        freopen(File".out","w",stdout);
    }
    inline int read(){
        int x=0,f=1;   char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
        return x*f;
    }
    const int mxn = 6e3+5;
    int n;
    vector<int> son[mxn];
    int h[mxn],f[mxn][2];
    bool v[mxn]/*1 参加 0 不参加*/;
    inline void dfs(int x){
        f[x][0] = 0;//不参加的快乐指数之和
        f[x][1] = h[x];
        for(int i = 0;i < son[x].size(); ++i){
            int y = son[x][i];
            dfs(y);
            f[x][0] += max(f[y][0],f[y][1]);
            f[x][1] += f[y][0];
        }
    }
    #define pb push_back
    inline int Max(int x,int y){ return x>y?x:y;}
    int main(){
    //    file();
        n = read();
        rep(i,1,n) h[i] = read();
        rep(i,1,n-1){
            int x = read(),y = read();
            v[x] = 1;
            son[y].pb(x);
        }
        int rt;
        rep(i,1,n)
            if(!v[i]){
                rt = i;
                break;
            }
        dfs(rt);
        cout << max(f[rt][0],f[rt][1]) << endl;
        return 0;
    }
    /*
    7
    1
    1
    1
    1
    1
    1
    1
    1 3
    2 3
    6 4
    7 4
    4 5
    3 5
    0 0
    */
    View Code
  • 相关阅读:
    委托事件
    泛型
    栈和队列
    泛型
    枚举与位枚举
    数组 集合
    .NET Framework 简介
    三行代码 完美解决word标签文字替换 POI增强版 可插入图片
    Github国内镜像网站,解决Github访问的神器
    Eureka
  • 原文地址:https://www.cnblogs.com/ve-2021/p/10805403.html
Copyright © 2011-2022 走看看