P1352求调(悬关
  • 板块题目总版
  • 楼主Accepetd
  • 当前回复10
  • 已保存回复10
  • 发布时间2024/12/6 20:21
  • 上次更新2024/12/6 22:01:13
查看原帖
P1352求调(悬关
1072849
Accepetd楼主2024/12/6 20:21

一运行电脑就显示超时

问题应该是出在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]);
}
2024/12/6 20:21
加载中...