QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#766068 | #5041. Fire | Kevin5307 | WA | 1ms | 3604kb | C++23 | 1.6kb | 2024-11-20 16:04:49 | 2024-11-20 16:04:50 |
Judging History
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],sum+1+lim[a]);
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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3592kb
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: 3604kb
input:
3 1 1 2 1 3 2 10 10
output:
-1
result:
ok 1 number(s): "-1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
2 0 1 2 10 10
output:
8
result:
ok 1 number(s): "8"
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 3544kb
input:
3 0 1 2 3 1 10 8 10
output:
6
result:
wrong answer 1st numbers differ - expected: '5', found: '6'