QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#662655#5418. Color the TreeAndycraftWA 35ms14200kbC++203.0kb2024-10-21 08:41:472024-10-21 08:41:48

Judging History

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

  • [2024-10-21 08:41:48]
  • 评测
  • 测评结果:WA
  • 用时:35ms
  • 内存:14200kb
  • [2024-10-21 08:41:47]
  • 提交

answer

#include <iostream>
#include <cassert>
#include <vector>
typedef long long LL;
template <class T> using Arr = std::vector<T>;
template <class T> struct Wnd : Arr<T> {
	Arr<int> history;
	inline T &operator[](size_t x) { return Arr<T>::operator[]((x + this->size()) % this->size()); }
	void push_back(const T &x) { history.push_back(x); Arr<T>::push_back(x); }
};

const int MAXN = 100002;
int n, a[MAXN], dep[MAXN], dfn[MAXN], fa[20][MAXN];
Arr<int> e[MAXN], dbuk[MAXN], g[MAXN];
int st[20][MAXN];
LL f[MAXN];

void add_dir(int u, int v, Arr<int> e[]) {
	// printf("add %d %d\n", u, v);
	e[u].push_back(v);
}

void add(int u, int v) {
	e[u].push_back(v);
	e[v].push_back(u);
}

void dfs(int p) {
	for (int i = 1; i < 20; ++i)
		fa[i][p] = fa[i - 1][fa[i - 1][p]];
	dfn[p] = ++dfn[0];
	dbuk[dep[p]].push_back(p);
	for (auto to : e[p])
		if (to != fa[0][p]) {
			dep[to] = dep[p] + 1;
			fa[0][to] = p;
			dfs(to);
		}
}

int ask_min(int l, int r) {
	int len = r - l;
	int p = 0;
	while ((1 << (p + 1)) <= len)
		++p;
	// printf("askmin %d %d\n", l, r);
	// printf("askmin (%d %d) = %d\n", l, r, std::min(st[p][l], st[p][r - (1 << p)]));
	return std::min(st[p][l], st[p][r - (1 << p)]);
}

int LCA(int u, int v) {
	if (dep[u] < dep[v])
		std::swap(u, v);
	for (int i = 20 - 1; i >= 0; --i)
		if (dep[u] - (1 << i) >= dep[v])
			u = fa[i][u];
	if (u == v)
		return u;
	for (int i = 20 - 1; i >= 0; --i)
		if (fa[i][u] != fa[i][v])
			u = fa[i][u], v = fa[i][v];
	return fa[0][u];
}

void dfs1(int p, const int &D) {
	// printf("dfs1 %d %d\n", p, D);
	if (dep[p] == D)
		f[p] = a[0];
	else {
		f[p] = 0;
		for (auto to : g[p]) {
			dfs1(to, D);
			f[p] += f[to];
		}
		f[p] = std::min(f[p], (LL)ask_min(1, D - dep[p] + 1));
	}
}

LL work(int depth) {
	Wnd<int> sta;
	Arr<int> &now = dbuk[depth];
	sta.push_back(1);
	for (int i = 0; i < (int)now.size(); ++i) {
		int d = LCA(sta[-1], now[i]);
		while (dfn[sta[-1]] > dfn[d]) {
			if (dfn[sta[-2]] > dfn[d])
				add_dir(sta[-2], sta[-1], g);
			else
				add_dir(d, sta[-1], g);
			sta.pop_back();
		}
		if (sta[-1] != d)
			sta.push_back(d);
		if (sta[-1] != now[i])
			sta.push_back(now[i]);
	}
	while (sta.size() >= 2)
		add_dir(sta[-2], sta[-1], g), sta.pop_back();

	// printf("# ");
	// for (auto x : sta.history)
	// 	printf("%d ", x);
	// puts("");

	assert(sta.size() == 1);
	dfs1(sta[0], depth);

	for (auto x : sta.history)
		g[x].clear();

	return f[sta[0]];
}

void Solve() {
	std::cin >> n;
	for (int i = 0; i < n; ++i) {
		std::cin >> a[i];
		st[0][i] = a[i];
	}
	for (int j = 1; j < 20; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i)
			st[j][i] = std::min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
	for (int i = 1; i < n; ++i) {
		int u, v;
		std::cin >> u >> v;
		add(u, v);
	}
	dfn[0] = 0;
	dfs(1);
	LL ans = 0;
	for (int i = 0; i < n; ++i)
		if (dbuk[i].size())
			ans += work(i);
	printf("%lld\n", ans);

	dfn[0] = 0;
	for (int i = 0; i < n; ++i)
		dbuk[i].clear();
	for (int i = 1; i <= n; ++i)
		e[i].clear();
}

int main() {
	std::ios::sync_with_stdio(false);
	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: 2ms
memory: 12008kb

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: 35ms
memory: 14200kb

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:

138
92
131
179
127
227
221
104
100
79
139
73
129
91
143
81
153
179
104
127
111
99
75
173
174
267
128
168
113
87
182
177
79
246
197
143
150
179
121
112
109
114
165
123
220
144
122
73
149
59
56
31
115
131
165
112
95
163
53
95
172
134
169
134
211
58
151
231
80
294
142
130
122
144
197
253
72
202
65
193
...

result:

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