BZOJ 3784: 树上的路径

Description

给定一个N个结点的树,结点用正整数1..N编号。每条边有一个正整数权值。用d(a,b)表示从结点a到结点b路边上经过边的权值。其中要求a<b.将这n*(n-1)/2个距离从大到小排序,输出前M个距离值。

Input

第一行两个正整数N,M

下面N-1行,每行三个正整数a,b,c(a,b<=N,C<=10000)。表示结点a到结点b有一条权值为c的边。

Output

共M行,如题所述.

Sample Input

5 10

1 2 1

1 3 2

2 4 3

2 5 4

Sample Output

7

7

6

5

4

4

3

3

2

1

HINT

N<=50000,M<=Min(300000,n*(n-1) /2 )

Solution

二分第M大的路径长度,树分治check+求答案。

可以用vector来把sort后的distance存下来 能优化掉一个log。

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <map>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long LL;
const int maxn = 50005;
inline int getint() {
	int r = 0; bool z = true; char c = getchar();
	for (; c < '0' || c > '9'; c = getchar()) if (c == '-') z = false;
	for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
	if (z) return r;
	return -r;
}
int n, m, cnte, h[maxn], cnt;
vector<int> V[maxn], U[maxn];
struct edge_t { int to, next, w; } edge[maxn << 1];
inline void ins(int x, int y, int z) {
	edge[++cnte].to = y;
	edge[cnte].next = h[x];
	edge[cnte].w = z;
	h[x] = cnte;
}
namespace solve1 {
	bool vis[maxn];
	pair<int, int> res;
	int N, siz[maxn], dis[maxn];
	void find_root(int now, int father) {
		siz[now] = 1; int tmp = 1;
		for (int i = h[now]; i; i = edge[i].next)
			if (edge[i].to != father && !vis[edge[i].to]) {
				find_root(edge[i].to, now);
				siz[now] += siz[edge[i].to];
				tmp = max(tmp, siz[edge[i].to]);
			}
		tmp = max(tmp, N - siz[now]);
		res = min(res, make_pair(tmp, now));
	}
	int get_root(int now, int size) {
		N = size;
		res = make_pair(2147483647, 0);
		find_root(now, 0);
		return res.second;
	}
	void dfs(int now, int father, int distance) {
		siz[now] = 1; dis[now] = distance;
		for (int i = h[now]; i; i = edge[i].next) {
			if (vis[edge[i].to] || father == edge[i].to) continue;
			dfs(edge[i].to, now, distance + edge[i].w);
			siz[now] += siz[edge[i].to];
		}
	}
	void fetch(int now, int father, vector<int> &v) {
		v.push_back(dis[now]);
		for (int i = h[now]; i; i = edge[i].next) {
			if (vis[edge[i].to] || father == edge[i].to) continue;
			fetch(edge[i].to, now, v);
		}
	}
	void dac1(int now, int size) {
		now = get_root(now, size);
		vis[now] = true;
		dfs(now, 0, 0);
		fetch(now, 0, V[now]);
		sort(V[now].begin(), V[now].end(), greater<int>());
		for (int i = h[now]; i; i = edge[i].next)
			if (!vis[edge[i].to]) {
				fetch(edge[i].to, now, U[cnt]);
				sort(U[cnt].begin(), U[cnt].end(), greater<int>());
				++cnt;
			}
		for (int i = h[now]; i; i = edge[i].next)
			if (!vis[edge[i].to])
				dac1(edge[i].to, siz[edge[i].to]);
	}
	int calc(vector<int> &V, int key) {
		int N = V.size(), p2 = N - 1, ret = 0;
		for (int p1 = 0; p1 < N; ++p1) {
			while (p2 > p1 && V[p1] + V[p2] <= key) --p2;
			if (p1 >= p2) break;
			ret += p2 - p1;
		}
		return ret;
	}
	int check(int x) {
		int ret = 0;
		for (int i = 1; i <= n; ++i) ret += calc(V[i], x);
		for (int i = 0; i < cnt; ++i) ret -= calc(U[i], x);
		return ret;
	}
	int solve() {
		dac1(1, n);
		int L = 0, R = 500000000, mid;
		while (L < R) {
			mid = (L + R) >> 1;
			if (check(mid) < m) R = mid;
			else L = mid + 1;
		}
		return R;
	}
}
namespace solve2 {
	map<int, int, greater<int> > M;
	int calc(vector<int> &V, int key, int d) {
		int N = V.size(), p2 = N - 1, ret = 0;
		for (int p1 = 0; p1 < N; ++p1) {
			while (p2 > p1 && V[p1] + V[p2] <= key) --p2;
			if (p1 >= p2) break;
			for (int i = p1 + 1; i <= p2; ++i)
				M[V[i] + V[p1]] += d;
			ret += p2 - p1;
		}
		return ret;
	}
	void solve(int key) {
		int tmp = 0;
		for (int i = 1; i <= n; ++i) tmp += calc(V[i], key, 1);
		for (int i = 0; i < cnt; ++i) tmp -= calc(U[i], key, -1);
		for (map<int, int, greater<int> >::iterator i = M.begin(); i != M.end(); ++i)
			for (int j = 0; j < i->second; ++j)
				printf("%d\n", i->first);
		for (int i = tmp; i < m; ++i)
			printf("%d\n", key);
	}
}
int main() {
	int x, y, z;
	n = getint(); m = getint();
	for (int i = 1; i < n; ++i) {
		x = getint(); y = getint(); z = getint();
		ins(x, y, z); ins(y, x, z);
	}
	int K = solve1::solve();
	solve2::solve(K);
}