QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#358092#6514. Is it well known in Poland?Djangle162857WA 0ms3852kbC++202.0kb2024-03-19 17:11:562024-03-19 17:11:56

Judging History

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

  • [2024-03-19 17:11:56]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3852kb
  • [2024-03-19 17:11:56]
  • 提交

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 n = (!q->empty()) ? q->size() : 0;
			if (n <= 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 n = (!q->empty()) ? q->size() : 0;
		if (n == 0) {
			q->push(a);
			return *q;
		}
		ll b = q->top();
		if (a <= b) {
			q->pop();
			// cout << a << " " << b << endl;
			if (n % 2 == 0)
				ans += a - b;
			else
				ans += a - b;
			// cout << ans << endl;
			return *q;
		}
		q->push(a);
		return *q;
	};
	priority_queue<ll> q = dfs(1, 0);
	//cout << ans << endl;
	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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

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

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

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: -100
Wrong Answer
time: 0ms
memory: 3620kb

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:

4606987993

result:

wrong answer 1st numbers differ - expected: '5848746543', found: '4606987993'