1. 程式人生 > >【TYVJ 1052】沒有上司的舞會

【TYVJ 1052】沒有上司的舞會

space for 圖片 freopen lose org stdout name 代碼

【原題鏈接】傳送門

【調試出錯】

我代碼裏的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

【TYVJ 1052】沒有上司的舞會