一运行电脑就显示超时
问题应该是出在41行那里,但不知道咋办
代码如下
#include<bits/stdc++.h>
#define ad(k) (k=-~k)
#define F(i,a,b) for(register int i=a;i<=b;ad(i))
#define sb(k) (k=~-k)
#define D(i,a,b) for(register int i=a;i>=b;sb(i))
#define gc getchar()
namespace Fast_IO{
struct Read{
template<typename T> inline Read& operator>>(T &x){
int f=1;char ch=gc;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc;}
while(ch>='0'&&ch<='9'){x=(x*10)+(ch-'0');ch=gc;}
x*=f;return *this;
}
}CYC;
}using namespace std;
using namespace Fast_IO;
typedef long long LL;const int N=6*1e3+5;
int n,w[N],a[N][N],b[N],f[N][1],rt=1;bool vis[N];
inline int MAX(const int a,const int b){return a>b?a:b;}
inline int MIN(const int a,const int b){return a<b?a:b;}
inline void dfs(const int u){
f[u][1]=w[u];
F(i,1,b[u]){
int son=a[u][i];
dfs(son);
f[u][0]+=MAX(f[son][0],f[son][1]);
f[u][1]+=f[son][0];
}
}
main(){
CYC>>n;
F(i,1,n)CYC>>w[i];
F(i,1,n-1){
int x,y;CYC>>x>>y;
a[y][++b[y]]=x;
vis[x]=1;
}while(vis[rt]==1)ad(rt);dfs(rt);
cout<<MAX(f[rt][0],f[rt][1]);
}