QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#725498#5418. Color the TreeRepeaterWA 60ms3776kbC++204.1kb2024-11-08 18:19:022024-11-08 18:19:09

Judging History

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

  • [2024-11-08 18:19:09]
  • 评测
  • 测评结果:WA
  • 用时:60ms
  • 内存:3776kb
  • [2024-11-08 18:19:02]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr i64 INF = 1e10;

struct HLD
{
	int n;
	std::vector<int> siz, top, dep, fa, L, R, id;
	std::vector<std::vector<int>> adj;
	int cur;

	HLD() {}
	HLD(int n)
	{
		init(n);
	}

	void init(int n)
	{
		this->n = n;
		siz.resize(n);
		top.resize(n);
		dep.resize(n);
		fa.resize(n);
		L.resize(n);
		R.resize(n);
		id.resize(n);

		cur = 0;
		adj.assign(n, {});
	}

	void add(int u, int v)
	{
		adj[u].emplace_back(v);
		adj[v].emplace_back(u);
	}

	void work(int root = 0)
	{
		top[root] = root;
		dep[root] = 0;
		fa[root] = -1;
		dfs1(root);
		dfs2(root);
	}

	void dfs1(int x)
	{
		if(fa[x] != -1)
			adj[x].erase(std::find(adj[x].begin(), adj[x].end(), fa[x]));

		siz[x] = 1;
		for(auto &y : adj[x])
		{
			fa[y] = x;
			dep[y] = dep[x] + 1;
			dfs1(y);

			siz[x] += siz[y];
			if(siz[y] > siz[adj[x][0]]) std::swap(y, adj[x][0]);
		}
	}

	void dfs2(int x)
	{
		L[x] = cur++;
		id[L[x]] = x;

		for(auto y : adj[x])
		{
			top[y] = (y == adj[x][0] ? top[x] : y);
			dfs2(y);
		}
		R[x] = cur;
	}

	int lca(int x, int y)
	{
		while(top[x] != top[y])
		{
			if(dep[top[x]] > dep[top[y]]) x = fa[top[x]];
			else y = fa[top[y]];
		}

		return dep[x] < dep[y] ? x : y;
	}
};

struct ST
{
	std::vector<std::vector<int>> f;

	ST(std::vector<int> &a)
	{
		int n = a.size(), logn = std::__lg(n);
		f.assign(n, std::vector<int>(logn + 1));

		for(int i = 0; i < n; i++) f[i][0] = a[i];
		for(int j = 0; j < logn; j++)
			for(int i = 0; i + (1 << (j + 1)) - 1 < n; i++)
				f[i][j + 1] = std::min(f[i][j], f[i + (1 << j)][j]);
	}

	int operator()(int l, int r)
	{
		if(l == r) return 0;
		int log = std::__lg(r - l);
		return std::min(f[l][log], f[r - (1 << log)][log]);
	}
};

void solve()
{
	int n; std::cin >> n;
	std::vector<int> a(n);
	for(int i = 0; i < n; i++) std::cin >> a[i];
	ST st(a);

	HLD tr(n);
	for(int i = 1; i < n; i++)
	{
		int u, v; std::cin >> u >> v;
		u--, v--;
		tr.add(u, v);
	}
	tr.work();

	std::vector<std::vector<int>> vec(n);
	for(int i = 0; i < n; i++) 
		vec[tr.dep[i]].emplace_back(i);

	std::vector<std::vector<int>> adj(n);
	auto cacl = [&](int D) -> i64
	{
		if(D) vec[D].emplace_back(0);
		std::sort(vec[D].begin(), vec[D].end(), [&](int x, int y)
			{
				return tr.L[x] < tr.L[y];
			});

		std::vector<int> tmp;
		for(int i = 0; i + 1 < (int)vec[D].size(); i++)
		{
			tmp.emplace_back(vec[D][i]);
			tmp.emplace_back(tr.lca(vec[D][i], vec[D][i + 1]));
		}
		tmp.emplace_back(vec[D].back());
		std::sort(tmp.begin(), tmp.end(), [&](int x, int y)
			{
				return tr.L[x] < tr.L[y];
			});
		tmp.erase(std::unique(tmp.begin(), tmp.end()), tmp.end());
		for(auto i : tmp) adj[i].clear();

		for(int i = 1; i < (int)tmp.size(); i++)
		{
			int lca = tr.lca(tmp[i], tmp[i - 1]);
			adj[lca].emplace_back(tmp[i]);
			adj[tmp[i]].emplace_back(lca);
		}
		// for(int i = 0; i < n; i++)
		// {
		// 	std::cout << i << ": ";
		// 	for(auto j : adj[i]) std::cout << j << " ";
		// 	std::cout << "\n";
		// }

		auto dfs = [&](auto dfs, int x, int fa) -> i64
		{
			i64 res = 0;
			if(fa != -1 && adj[x].size() == 1) res = INF;
			else
			{
				for(auto y : adj[x])
				{
					if(y == fa) continue;
					res += dfs(dfs, y, x);
				}
			}

			// if(fa != -1) std::cout << "D: " << D - tr.dep[x] << " " << D - tr.dep[fa] << "\n";
			// std::cerr << D - tr.dep[x] << " " << D - tr.dep[fa] << "\n";
			if(fa != -1) 
			{
				// std::cerr << "D: " << x << " " << tr.dep[x] << " " << fa << " " << tr.dep[fa] << " ";
				// std::cerr << st(D - tr.dep[x], D - tr.dep[fa] + 1) << "\n";
				res = std::min<i64>(res, st(D - tr.dep[x], D - tr.dep[fa] + 1));
			}

			return res;
		};
		return dfs(dfs, 0, -1);
	};

	i64 ans = a[0];
	for(int i = 1; i < n; i++) if(vec[i].size()) 
	{
		ans += cacl(i);
		// std::cout << i << ": " << ans << "\n";
 	}

	std::cout << ans << "\n";
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int t; std::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: 3664kb

input:

3
4
10 15 40 1
1 2
2 3
2 4
5
10 5 1 100 1000
1 2
2 3
2 4
4 5
4
1000 200 10 8
1 2
2 3
3 4

output:

35
17
1218

result:

ok 3 number(s): "35 17 1218"

Test #2:

score: -100
Wrong Answer
time: 60ms
memory: 3776kb

input:

3000
54
43 44 11 49 17 14 7 30 35 12 34 14 15 8 29 47 30 31 39 17 26 23 26 45 34 50 49 13 35 18 29 15 13 44 47 5 22 20 19 46 30 22 26 13 47 46 43 27 48 48 13 14 30 44
1 2
1 3
2 4
3 5
4 6
2 7
4 8
1 9
3 10
1 11
8 12
2 13
9 14
1 15
1 16
15 17
15 18
7 19
1 20
19 21
13 22
10 23
17 24
9 25
9 26
24 27
8 28...

output:

520
309
569
461
229
494
619
180
118
185
155
120
359
227
404
276
264
528
280
275
168
264
94
538
405
401
392
496
125
345
467
473
163
346
363
529
474
197
307
309
135
169
739
341
611
475
536
127
320
59
56
67
240
307
462
206
167
537
60
426
357
194
321
200
825
124
265
532
138
825
301
161
188
303
537
666
1...

result:

wrong answer 1st numbers differ - expected: '180', found: '520'