QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#623701#9110. Zayin and Treeexpectant#AC ✓215ms41964kbC++141.5kb2024-10-09 13:46:202024-10-09 13:46:21

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 13:46:21]
  • 评测
  • 测评结果:AC
  • 用时:215ms
  • 内存:41964kb
  • [2024-10-09 13:46:20]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=j;i<=(k);++i)
#define drp(i,j,k) for(int i=j;i>=(k);--i)
#define MAXN 1000005
typedef long long ll;
inline int read(){
	int x=0;
	bool sgn=true;
	char ch=getchar();
	while(!isdigit(ch)) sgn^=ch=='-',ch=getchar();
	while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return sgn?x:-x;
}
inline void chkmin(int &x,int y){x=std::min(x,y);}
int n,ans,a[MAXN],dep[MAXN],val[MAXN];
std::vector<int> edge[MAXN];
inline void init(){
	ans=1e9;
	rep(i,1,n) a[i]=dep[i]=val[i]=0;
	rep(i,1,n) std::vector<int>().swap(edge[i]);
}
inline void dfs1(int u,int fa){
	dep[u]=dep[fa]+1,val[u]=dep[u]-a[u];
	for(auto v:edge[u]){
		if(v==fa) continue;
		dfs1(v,u),chkmin(val[u],val[v]);
	}
}
inline void dfs2(int u,int fa,int ret){
	chkmin(ret,1-a[u]);
	chkmin(ans,a[u]+ret);
	chkmin(ans,a[u]+val[u]-dep[u]+1);
	std::set<std::pair<int,int>> set;
	set.insert(std::make_pair((int)1e9,0));
	for(auto v:edge[u]) if(v!=fa) set.insert(std::make_pair(val[v]-dep[u]+1,v));
	for(auto v:edge[u]){
		if(v==fa) continue;
		auto it=set.begin();
		if(it->second==v) ++it;
		dfs2(v,u,std::min(ret,it->first)+1);
	}
}
int main(){
	std::ios::sync_with_stdio(0);
	std::cin.tie(0),std::cout.tie(0);
	drp(task,read(),1){
		n=read(),init();
		rep(i,1,n) a[i]=read();
		rep(i,1,n-1){
			int u=read(),v=read();
			edge[u].push_back(v);
			edge[v].push_back(u);
		}
		dfs1(1,0),dfs2(1,0,(int)1e9);
		std::cout<<ans<<'\n';
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 215ms
memory: 41964kb

input:

3009
5
4 5 3 4 2
1 2
2 3
3 4
3 5
5
4 4 1 1 2
1 2
2 3
3 4
3 5
10
5 8 1 0 8 7 5 2 0 4
2 4
3 8
3 9
1 2
1 3
3 6
4 5
5 7
6 10
10
6 8 8 4 8 0 6 6 0 2
7 10
1 7
2 9
2 3
3 4
1 5
1 6
6 8
1 2
10
9 0 4 0 4 6 0 2 0 0
1 5
1 3
1 7
2 6
1 2
1 9
1 4
5 8
7 10
10
8 8 1 2 7 4 8 6 0 8
1 6
1 7
1 5
7 9
1 3
1 2
2 10
3 4
1 8...

output:

0
-1
-6
-6
-7
-6
-7
-4
-3
-7
-5
-6
-5
-4
-6
-3
-4
-7
-4
-4
-6
-6
-6
-5
-4
-5
-6
-6
-7
-7
-5
-7
-6
-6
-7
-6
-5
-5
-4
-6
-6
-5
-6
-6
-6
-6
-3
-6
-3
-6
-4
-6
-7
-6
-7
-6
-6
-5
-7
-6
-4
-7
-3
-5
-5
-6
-4
-5
-7
-6
-5
-5
-4
-3
-5
-3
-4
-2
-6
-5
-7
-4
-5
-5
-7
-7
-4
-6
-5
-4
-6
-5
-5
-6
-3
-6
-7
-7
-7
-6
-...

result:

ok 3009 lines