QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#766072#5041. FireKevin5307WA 1ms3872kbC++231.6kb2024-11-20 16:06:102024-11-20 16:06:11

Judging History

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

  • [2024-11-20 16:06:11]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3872kb
  • [2024-11-20 16:06:10]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
int n;
ll k;
vector<int> G[100100];
int siz[100100];
ll a[100100];
void dfs(int u,int fa)
{
	siz[u]=1;
	for(auto v:G[u])
		if(v!=fa)
		{
			dfs(v,u);
			siz[u]+=siz[v];
		}
}
ll lim[100100];
void dfs2(int u,int fa)
{
	for(auto v:G[u])
		if(v!=fa)
			dfs2(v,u);
	vector<int> vec;
	for(auto v:G[u])
		if(v!=fa)
			vec.pb(v);
	sort(ALL(vec),[&](int a,int b){return lim[a]+siz[a]*2<lim[b]+siz[b]*2;});
	ll sum=0;
	for(auto a:vec)
	{
		lim[u]=min(lim[u],lim[a]-sum-1);
		sum+=siz[a]*2;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		G[u].pb(v);
		G[v].pb(u);
	}
	for(int i=1;i<=n;i++)
		cin>>a[i];
	dfs(1,0);
	for(int i=1;i<=n;i++)
	{
		lim[i]=a[i];
		if(i==1) lim[i]=1ll*inf*inf;
		if(i>1) lim[i]=min(lim[i],a[i]+k-2-2*(siz[i]-1));
		else lim[i]=min(lim[i],a[i]+k-2*(siz[i]-1));
	}
	dfs2(1,0);
	cout<<max(-1ll,lim[1])<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3572kb

input:

3 1
1 2
1 3
4 3 5

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3872kb

input:

3 1
1 2
1 3
2 10 10

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 3632kb

input:

2 0
1 2
10 10

output:

7

result:

wrong answer 1st numbers differ - expected: '8', found: '7'