QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#122230#2065. Cyclic DistanceErayWA 22ms24964kbC++141.8kb2023-07-09 20:27:482023-07-09 20:27:49

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-09 20:27:49]
  • 评测
  • 测评结果:WA
  • 用时:22ms
  • 内存:24964kb
  • [2023-07-09 20:27:48]
  • 提交

answer

#include <bits/stdc++.h>

typedef long long LL;

const int N = 2e5 + 5;

int n, K;
struct Edge { int to, nxt, w; } edge[N << 1];
int head[N], ek;
void add_edge(int u, int v, int w) { edge[ek] = (Edge){v, head[u], w}, head[u] = ek++; }

struct Convex {
	std::priority_queue<LL, std::vector<LL>, std::greater<LL>> vl, vr;
	LL vm, cl, cr;
	int sz;
	void clear() {
		while(!vl.empty()) vl.pop();
		while(!vr.empty()) vr.pop();
		vm = cl = cr = sz = 0;
	}
	void swap(Convex &rhs) {
		vl.swap(rhs.vl), vr.swap(rhs.vr);
		std::swap(vm, rhs.vm), std::swap(cl, rhs.cl), std::swap(cr, rhs.cr);
	}
	void insert(LL x) {
		if(sz <= K / 2 || x > vm) vl.push(x - cl);
		else vr.push(x - cr);
		sz++;
		if((int)vl.size() > K / 2) {
			if(sz >= K / 2 + 2) vr.push(vm - cr);
			vm = vl.top() + cl, vl.pop();
		}
	}
};
void merge(Convex &p, Convex &q) {
	if(p.sz < q.sz) p.swap(q);
	while(!q.vl.empty()) p.insert(q.vl.top() + q.cl), q.vl.pop();
	if(q.sz > K / 2) p.insert(q.vm);
	while(!q.vr.empty()) p.insert(q.vr.top() + q.cr), q.vr.pop();
	while(p.sz > K) p.sz--, p.vr.pop();
}

Convex f[N];
void dfs(int u, int fa) {
	f[u].insert(0);
	for(int i = head[u]; i; i = edge[i].nxt) if(edge[i].to != fa) {
		int v = edge[i].to; LL w = 2 * edge[i].w;
		dfs(v, u);
		f[v].cl += w, f[v].cr -= w;
		if(!(K & 1)) f[v].vm -= w;
		merge(f[u], f[v]);
	}
}

int main() {
	int T; scanf("%d", &T);
	while(T--) {
		ek = 1;
		scanf("%d%d", &n, &K);
		for(int i = 1; i <= n; i++) head[i] = 0;
		for(int i = 1; i < n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add_edge(u, v, w), add_edge(v, u, w); }
		for(int i = 1; i <= n; i++) f[i].clear();
		dfs(1, 0);
		LL ans = f[1].vm;
		while(!f[1].vl.empty()) ans += f[1].vl.top(), f[1].vl.pop();
		while(!f[1].vr.empty()) ans += f[1].vr.top(), f[1].vr.pop();
		printf("%lld\n", ans);
	}
	return 0;
}

详细

Test #1:

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

input:

1
5 3
1 2 4
1 3 1
1 4 8
4 5 9

output:

44

result:

ok single line: '44'

Test #2:

score: -100
Wrong Answer
time: 22ms
memory: 24964kb

input:

57206
3 2
1 2 574927
2 3 406566
2 2
1 2 308806
4 3
1 2 312588
2 3 500141
2 4 53602
3 3
1 2 797183
2 3 944061
5 3
1 2 624010
2 3 488613
3 4 734514
4 5 497493
5 4
1 2 540278
2 3 488655
2 4 228989
2 5 653896
2 2
1 2 569117
4 2
1 2 821764
2 3 499471
1 4 549060
2 2
1 2 991159
2 2
1 2 482941
5 4
1 2 30462...

output:

813132
617612
1625458
3482488
8604542
2285102
1138234
3740590
1982318
965882
3418504
5026562
1623414
1885106
1952142
2088464
1691896
3102076
3680054
3071842
5697196
5623230
167344
2500212
1985748
1358950
1182198
2273662
2331560
1088442
7382806
2373066
4704084
3104226
1917330
3162310
4771466
3669186
...

result:

wrong answer 1st lines differ - expected: '1962986', found: '813132'