QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#766207#5041. FireKevin5307WA 49ms12144kbC++232.4kb2024-11-20 16:35:022024-11-20 16:35:08

Judging History

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

  • [2024-11-20 16:35:08]
  • 评测
  • 测评结果:WA
  • 用时:49ms
  • 内存:12144kb
  • [2024-11-20 16:35:02]
  • 提交

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];
ll lim2[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;
	}
}
void dfs3(int u,int fa)
{
	for(auto v:G[u])
		if(v!=fa)
			dfs3(v,u);
	vector<int> vec;
	for(auto v:G[u])
		if(v!=fa)
			vec.pb(v);
	if(!sz(vec))
		return ;
	sort(ALL(vec),[&](int a,int b){return lim[a]+siz[a]*2<lim[b]+siz[b]*2;});
	vector<ll> v2;
	ll sum=0;
	for(auto a:vec)
	{
		v2.pb(lim[a]-sum-1);
		sum+=siz[a]*2;
	}
	vector<ll> pre(sz(v2)+1),suf(sz(v2)+1);
	pre[0]=suf.back()=1ll*inf*inf;
	for(int i=0;i<sz(v2);i++)
		pre[i+1]=min(pre[i],v2[i]);
	for(int i=sz(v2)-1;i>=0;i--)
		suf[i]=min(suf[i+1],v2[i]);
	ll mx=-1ll*inf*inf;
	for(int i=0;i<sz(vec);i++)
	{
		int v=vec[i];
		ll val=min({lim2[u],lim2[v]-(sum-siz[v]*2)-1,pre[i],suf[i+1]+siz[v]*2,a[u]+k-2-(sum-siz[v]*2)});
		mx=max(mx,val);
	}
	lim2[u]=mx;
}
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;
		lim2[i]=lim[i];
		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);
	dfs3(1,0);
	cout<<max(-1ll,lim2[1])<<endl;
	return 0;
}

详细

Test #1:

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

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: 5580kb

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: 3544kb

input:

2 0
1 2
10 10

output:

8

result:

ok 1 number(s): "8"

Test #4:

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

input:

3 0
1 2
3 1
10 8 10

output:

5

result:

ok 1 number(s): "5"

Test #5:

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

input:

10 0
9 7
2 7
2 1
4 7
9 6
7 3
8 10
9 10
8 5
29 24 27 22 20 25 23 29 20 20

output:

8

result:

ok 1 number(s): "8"

Test #6:

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

input:

100 0
82 86
79 53
86 24
64 74
3 26
65 91
47 35
73 90
8 75
59 27
78 19
76 20
34 7
82 31
11 20
4 85
52 16
46 51
93 52
87 20
82 6
64 92
52 2
54 62
60 63
69 95
24 90
54 25
9 72
2 20
27 82
29 25
43 85
57 92
36 77
22 81
14 84
69 49
20 47
52 98
97 88
23 45
1 87
56 45
31 36
85 31
44 83
33 53
10 78
37 40
13 ...

output:

52

result:

ok 1 number(s): "52"

Test #7:

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

input:

1000 0
781 478
708 997
164 3
242 868
210 828
349 149
376 785
332 690
182 514
552 322
461 542
124 559
402 695
99 989
590 295
540 310
115 624
903 158
915 162
832 238
382 796
860 198
620 262
815 436
494 546
162 173
853 376
731 770
444 702
337 722
995 306
839 489
464 843
761 309
562 88
27 852
192 530
19...

output:

208

result:

ok 1 number(s): "208"

Test #8:

score: 0
Accepted
time: 5ms
memory: 6208kb

input:

10000 0
8532 1309
7924 7641
3850 2342
5333 6578
4919 6615
2937 1134
7202 3833
3118 5150
3032 423
5684 647
2038 5418
7810 4255
6912 8228
6136 7472
1889 4121
9348 7211
8439 4505
9915 7800
5500 8587
4261 8149
9496 243
8502 4248
4838 3073
994 3446
5177 3384
2216 9983
3930 2243
6638 5154
1991 3731
4216 1...

output:

800

result:

ok 1 number(s): "800"

Test #9:

score: 0
Accepted
time: 49ms
memory: 11844kb

input:

100000 0
7792 26010
85804 1078
82899 25724
47158 81804
60588 36038
7704 64781
50035 14650
92695 7665
27387 75724
19543 53096
50134 64624
77873 18941
18330 34916
31993 22809
40207 54229
55324 55105
16997 96779
63858 97958
40532 31166
13028 2225
92083 90328
39005 67586
66623 36043
84284 79134
84722 35...

output:

3231

result:

ok 1 number(s): "3231"

Test #10:

score: 0
Accepted
time: 48ms
memory: 12144kb

input:

100000 0
84175 9629
86499 8316
10499 32549
6203 8049
73531 21696
59782 18131
48682 49498
84503 78323
35878 37155
72866 95140
70292 39353
65243 8130
33496 52738
74044 44275
15789 35130
55929 34691
71587 2066
14877 70981
76619 50488
89347 78338
57879 13747
47955 45641
98403 44863
90240 41682
67754 110...

output:

-1

result:

ok 1 number(s): "-1"

Test #11:

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

input:

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

output:

-1

result:

ok 1 number(s): "-1"

Test #12:

score: 0
Accepted
time: 0ms
memory: 5832kb

input:

2962 0
1802 1648
2780 474
20 2923
1902 2515
2650 1040
31 74
1753 392
1635 2719
2900 1286
1158 1227
1881 2316
1983 290
881 724
2904 1365
574 118
167 1726
2029 1208
2731 2451
113 2012
1763 2666
1489 145
1165 2201
2691 2141
126 2903
194 1476
2345 1025
1809 1964
1406 2362
305 1192
2007 1415
2957 1122
22...

output:

38596

result:

ok 1 number(s): "38596"

Test #13:

score: 0
Accepted
time: 26ms
memory: 9340kb

input:

58346 0
32606 33231
10774 14178
56179 20149
4402 53574
30314 6332
47840 23093
49961 30471
1558 7974
981 26443
44800 53897
7501 56273
24880 56072
21534 32688
56597 49273
29017 29780
4351 49319
29361 20740
2603 15303
57792 6137
8823 41985
25290 18398
57356 23079
15412 16730
27348 1294
28703 15186
1476...

output:

199014

result:

ok 1 number(s): "199014"

Test #14:

score: 0
Accepted
time: 5ms
memory: 6376kb

input:

11327 0
6972 5303
4106 6016
10927 1269
10358 846
8138 7882
5083 2862
1623 4163
10328 10962
6629 2486
6706 5247
155 1179
6717 9319
918 11081
7491 10594
3042 7403
1275 100
5567 10436
8364 11318
8532 4071
3070 9038
9635 4091
233 9658
1127 6631
37 5223
4314 1203
8162 8875
9962 10547
7578 9494
10083 9960...

output:

130321

result:

ok 1 number(s): "130321"

Test #15:

score: -100
Wrong Answer
time: 0ms
memory: 5552kb

input:

2 805038099
2 1
5 9

output:

8

result:

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