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
| #include <bits/stdc++.h> #define MAX_N 50000 #define MAX_M 800000 #define LOG Log[t-s] using namespace std; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, k, rt, tot; int sz[MAX_N+5], w[MAX_N+5], L[MAX_M+5], R[MAX_M+5]; int d[MAX_M+5], st[MAX_M+5][25], Log[MAX_M+5]; struct node {int p, l, r, val; bool operator < (const node &t) const {return t.val > val;}} ; vector <int> G[MAX_N+5], E[MAX_N+5]; bool mrk[MAX_N+5]; priority_queue <node> que; void insert(int u, int v, int c) {G[u].push_back(v), E[u].push_back(c);} void addedge(int u, int v, int c) {insert(u, v, c), insert(v, u, c);} void getrt(int u, int fa) { sz[u] = 1, w[u] = 0; for (int i = 0, v; i < (int)G[u].size(); i++) if (((v = G[u][i]) ^ fa) && !mrk[v]) getrt(v, u), sz[u] += sz[v], w[u] = max(w[u], sz[v]); if ((w[u] = max(w[u], tot-sz[u])) < w[rt]) rt = u; } void getdis(int u, int fa, int dis) { d[++m] = dis, L[m] = L[m-1]; if (!R[m]) R[m] = R[m-1]; for (int i = 0, v; i < (int)G[u].size(); i++) if (((v = G[u][i]) ^ fa) && !mrk[v]) getdis(v, u, dis+E[u][i]); } void DFS(int u) { d[++m] = 0, L[m] = m, R[m] = m-1, mrk[u] = true; for (int i = 0, v; i < (int)G[u].size(); i++) if (!mrk[v = G[u][i]]) R[m+1] = m, getdis(v, u, E[u][i]); for (int i = 0, v; i < (int)G[u].size(); i++) if (!mrk[v = G[u][i]]) w[rt = 0] = tot = sz[v], getrt(v, u), DFS(rt); } int Max(int a, int b) {return d[a] > d[b] ? a : b;} void init_ST() { for (int i = 2; i <= m; i++) Log[i] = Log[i>>1]+1; for (int i = 1; i <= m; i++) st[i][0] = i; for (int j = 1; (1<<j) <= m; j++) for (int i = 1; i <= m-(1<<j)+1; i++) st[i][j] = Max(st[i][j-1], st[i+(1<<(j-1))][j-1]); } int query(int s, int t) {return s > t ? -1 : Max(st[s][LOG], st[t-(1<<LOG)+1][LOG]);} int main() { read(n), read(k); for (int i = 1, u, v, c; i < n; i++) read(u), read(v), read(c), addedge(u, v, c); w[rt = 0] = tot = n, getrt(1, 0), DFS(rt), init_ST(); for (int i = 1; i <= m; i++) if (L[i] <= R[i]) que.push((node){i, L[i], R[i], d[i]+d[query(L[i], R[i])]}); for (int i = 1, t, p; i <= k; i++) { node tp = que.top(); que.pop(), p = tp.p, printf("%d\n", tp.val); int ll = tp.l, rr = tp.r, lr = query(ll, rr)-1, rl = query(ll, rr)+1; if (~(t = query(ll, lr))) que.push((node){p, ll, lr, d[p]+d[t]}); if (~(t = query(rl, rr))) que.push((node){p, rl, rr, d[p]+d[t]}); } return 0; }
|