QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#358101#6514. Is it well known in Poland?Djangle162857WA 31ms18788kbC++201.9kb2024-03-19 17:17:152024-03-19 17:17:16

Judging History

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

  • [2024-03-19 17:17:16]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:18788kb
  • [2024-03-19 17:17:15]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " == " << x << endl
#define el '\n'
typedef long long ll;
const int mod = 1000000007;
const int inf = 2147483647;
const int N = 200020;
void solve()
{
	int n;
	ll sum = 0, ans = 0;
	cin >> n;
	vector<ll> val(n + 1);
	vector<vector<int>> edge(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> val[i];
		sum += val[i];
	}
	for (int i = 1, x, y; i < n; i++) {
		cin >> x >> y;
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	function<priority_queue<ll>(int, int)> dfs = [&](int x, int fa) {
		priority_queue<ll> *q = new priority_queue<ll>();
		ll a = val[x];
		for (int to : edge[x]) {
			if (to == fa)
				continue;
			priority_queue<ll> now = dfs(to, x);
			if (q->size() < now.size()) {
				swap(*q, now);
			}
			while (!now.empty()) {
				ll res = now.top();
				now.pop();
				q->push(res);
			}
		}
		/*priority_queue<ll> now = *q;
		debug(x);
		while (!now.empty()) {
			cout << now.top() << " ";
			now.pop();
		}
		cout << endl;*/
		while (1) {
			int siz = (!q->empty()) ? q->size() : 0;
			if (siz <= 1) {
				break;
			}
			int now = q->top();
			if (a >= now) {
				q->push(a);
				return *q;
			}
			ll b = q->top();
			q->pop();
			ll c = q->top();
			q->pop();
			a = a + c - b;
		}
		int siz = (!q->empty()) ? q->size() : 0;
		if (siz == 0) {
			q->push(a);
			return *q;
		}
		ll b = q->top();
		if (a <= b) {
			q->pop();
			if (n % 2 == 0)
				ans += a - b;
			else
				ans -= a - b;
			return *q;
		}
		q->push(a);
		return *q;
	};
	priority_queue<ll> q = dfs(1, 0);
	int tmp = 0;
	while (!q.empty()) {
		if ((tmp++) % 2 == 0)
			ans += q.top();
		else
			ans -= q.top();
		q.pop();
	}
	cout << (ans + sum) / 2 << endl;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int T = 1;
	// cin>>T;
	while (T--) {
		solve();
	}
	return 0;
}

詳細信息

Test #1:

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

input:

5
1 5 3 2 4
1 2
1 3
2 4
2 5

output:

7

result:

ok 1 number(s): "7"

Test #2:

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

input:

20
530933472 920863930 136179483 46535289 291417568 338674057 731327836 375973098 986104110 203163644 489326483 785212564 712578139 801609721 666347686 282530991 823910542 217884304 785578553 116701831
8 3
18 19
11 20
2 18
8 13
5 15
12 16
7 8
10 9
13 6
17 10
17 18
13 15
18 4
13 12
1 14
17 15
15 14
5...

output:

4611098449

result:

ok 1 number(s): "4611098449"

Test #3:

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

input:

20
384529189 217795442 901954855 742992564 354875060 497288585 40376145 575315239 263212445 574630916 520470316 917128880 461333242 407666839 565926566 390970156 568486150 291329847 493439854 637783217
10 17
19 8
20 17
7 17
9 6
17 14
16 18
4 3
9 14
6 2
14 18
13 4
11 15
9 3
7 12
16 1
8 14
5 14
9 15

output:

4410782058

result:

ok 1 number(s): "4410782058"

Test #4:

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

input:

19
601996737 696498327 385657564 527861058 529330573 376647612 223077352 338754937 682264670 671903443 645156487 755321105 978945836 120368247 611275923 947566064 98892994 858404931 401419272
11 14
8 14
3 6
13 12
16 15
1 4
11 4
2 7
4 3
15 5
2 1
13 5
10 16
9 16
1 17
18 6
3 19
5 7

output:

5848746543

result:

ok 1 number(s): "5848746543"

Test #5:

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

input:

1000
379354617 199235046 102686848 841958378 178689385 896777301 798019293 538540178 877449071 825184825 426375184 882476194 614078703 438824267 731949932 770345838 655572525 687098129 758287148 690715403 984914877 365231609 752380073 509777049 511312331 889780846 745450511 740863321 552773286 15071...

output:

252915929540

result:

ok 1 number(s): "252915929540"

Test #6:

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

input:

999
402468827 468124822 806603931 945584323 480102625 19722 169790452 515757998 19958108 528988204 415841643 111279190 885615782 740479752 550815713 556065700 511850711 148616044 552183365 471481958 946651162 968386282 610171866 971986310 377716133 564442897 222395758 16053401 613716892 487668831 48...

output:

249829986618

result:

ok 1 number(s): "249829986618"

Test #7:

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

input:

1000
637313417 62510663 83128345 702987667 374713776 778401168 619811617 896369345 486498325 448824176 297332011 916674955 895413407 803990450 21693588 866905388 169932411 340234047 259184127 331450756 506720779 194809709 896076578 936050255 925430496 741889050 638981221 628789074 358540287 89791215...

output:

226446817254

result:

ok 1 number(s): "226446817254"

Test #8:

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

input:

1000
147397255 520346638 619331018 431963137 427476094 995854324 640680706 473132459 1061281 701469953 87766202 316181634 506906587 181791922 174614534 639267313 489608085 693127159 412762229 906080005 562083964 319379642 148511208 32431198 908725162 531631359 390987308 965197199 851576914 95327968 ...

output:

229874635771

result:

ok 1 number(s): "229874635771"

Test #9:

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

input:

999
98690497 488296773 570509174 87593828 566189245 345232397 309963286 886305731 195509167 561473976 304758565 239428403 9228891 38443063 343536428 606219587 806177708 94853421 426729965 393545389 820769556 365126116 489908193 276668395 217091957 976510596 415255517 670037302 793997764 947495706 10...

output:

266569042607

result:

ok 1 number(s): "266569042607"

Test #10:

score: -100
Wrong Answer
time: 31ms
memory: 18788kb

input:

99999
986882907 1232240 337619783 537571360 365049288 188680042 509367962 534436294 946650473 29350838 923503765 618367016 168291380 404626206 124377948 986455184 473759137 387049360 162826460 323510191 211352878 82863775 17091396 407155225 173680347 196421120 814831494 60052678 758323119 332899343 ...

output:

26957015595379

result:

wrong answer 1st numbers differ - expected: '26955310538442', found: '26957015595379'