QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#139536#6514. Is it well known in Poland?UrgantTeamWA 3ms11296kbC++231.7kb2023-08-13 20:18:452023-08-13 20:18:49

Judging History

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

  • [2023-08-13 20:18:49]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:11296kb
  • [2023-08-13 20:18:45]
  • 提交

answer

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long double ld;
typedef long long ll;

vector <int> g[100005];
int metka[100005], value[100005];
multiset <ll> opa[100005];
ll DODIK = 0;

void dfs(int v, int pr)
{
	bool leaf = true;
	for (int u : g[v])
	{
	 	if (u == pr) continue;
	 	dfs(u, v);
	 	leaf = false;
	}

	if (leaf)
	{
	 	metka[v] = v;
	 	opa[v].insert(value[v]);
	 	return;
	}

	int mx = 0;
	for (int u : g[v])
	{
		if (u == pr) continue;
		if (mx == 0 || opa[metka[u]].size() > opa[metka[mx]].size()) mx = u; 	
	}
	metka[v] = metka[mx];

	for (int u : g[v])
	{
	 	if (u == pr || u == mx) continue;
	 	for (ll weight : opa[metka[u]]) opa[metka[v]].insert(weight);
	}

	int sign = -1;
	ll root = value[v];
	while (!opa[metka[v]].empty())
	{
	 	ll last = *opa[metka[v]].rbegin();
	 	if (last <= root) break;

	 	opa[metka[v]].erase(opa[metka[v]].find(last));
	 	root += sign * last;
	 	sign = -sign;
	}
	if (sign == 1) DODIK += root;
	else opa[metka[v]].insert(root);
	return;
}

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
	ios_base::sync_with_stdio(0); cin.tie(0);

	int n;
	cin >> n;

	ll sum = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> value[i];
		sum += value[i];
	}

	for (int i = 1; i < n; i++)
	{
	 	int u, v;
	 	cin >> u >> v;
	 	g[u].pb(v);
	 	g[v].pb(u);
	}
	dfs(1, -1);
 
  	if (n % 2 == 1) DODIK = -DODIK;
	int sign = 1;
	for (auto it = opa[metka[1]].rbegin(); it != opa[metka[1]].rend(); it++)
	{
	 	DODIK += sign * (*it);
	 	sign = -sign;
	}
	cout << (sum + DODIK) / 2 << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 11232kb

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

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:

4594607090

result:

wrong answer 1st numbers differ - expected: '4611098449', found: '4594607090'