N<=50000,M<=Min(300000,n*(n-1) /2 )
#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int maxn = 5E4 + 50; const int INF = ~0U>>1; struct E{ int to,w; E(int _to = 0,int _w = 0) {to = _to; w = _w;} }; struct data1{ int Max[16],pos[16]; data1() {memset(Max,0,sizeof(Max)); memset(pos,0,sizeof(pos));} }; struct data2{ int sum,l,r,Root,va,po; data2(int _sum = 0,int _l = 0,int _r = 0,int _Root = 0,int _va = 0,int _po = 0) { sum = _sum; l = _l; r = _r; Root = _Root; va = _va; po = _po; } bool operator < (const data2 &b) const {return va < b.va;} }; int n,m,dfs_clock,O,ma,dfn[maxn],out[maxn],siz[maxn],d[maxn]; bool Mark[maxn]; vector v[maxn]; vector v2[maxn]; priority_queue Q; void dfs2(int x,int from,int Siz) { int Max = 0; siz[x] = 1; for (int i = 0; i < v[x].size(); i++) { int to = v[x][i].to; if (to == from || Mark[to]) continue; dfs2(to,x,Siz); siz[x] += siz[to]; Max = max(Max,siz[to]); } Max = max(Max,Siz - siz[x]); if (Max < ma) ma = Max,O = x; } void dfs3(int x,int o,int from,int sum) { dfn[x] = dfs_clock++; data1 New; New.Max[0] = sum; New.pos[0] = dfn[x]; v2[o].push_back(New); for (int i = 0; i < v[x].size(); i++) { int to = v[x][i].to; if (to == from || Mark[to]) continue; dfs3(to,o,x,sum + v[x][i].w); } out[x] = dfs_clock - 1; } void dfs4(int x,int o,int from,int l,int r,int va,int po) { Q.push(data2(v2[o][dfn[x]].Max[0],l,r,o,va + v2[o][dfn[x]].Max[0],po)); for (int i = 0; i < v[x].size(); i++) { int to = v[x][i].to; if (to == from || Mark[to]) continue; dfs4(to,o,x,l,r,va,po); } } void dfs1(int x,int Siz) { if (Siz == 1) return; dfs_clock = 0; ma = INF; dfs2(x,0,Siz); int o = O; Mark[o] = 1; dfs3(o,o,0,0); for (int i = 1; i < 16; i++) for (int j = 0; j < dfs_clock; j++) { int k = j + (1<<(i-1)); if (k >= dfs_clock) break; if (v2[o][j].Max[i-1] > v2[o][k].Max[i-1]) v2[o][j].Max[i] = v2[o][j].Max[i-1],v2[o][j].pos[i] = v2[o][j].pos[i-1]; else v2[o][j].Max[i] = v2[o][k].Max[i-1],v2[o][j].pos[i] = v2[o][k].pos[i-1]; } for (int i = 0; i < v[o].size(); i++) { int to = v[o][i].to; if (Mark[to]) continue; int l = 0,r = dfn[to] - 1; int len = r - l + 1,va,po; int k = r - (1< v2[o][k].Max[d[len]]) va = v2[o][l].Max[d[len]],po = v2[o][l].pos[d[len]]; else va = v2[o][k].Max[d[len]],po = v2[o][k].pos[d[len]]; dfs4(to,o,0,l,r,va,po); } for (int i = 0; i < v[o].size(); i++) { int to = v[o][i].to; if (Mark[to]) continue; dfs1(to,siz[to]); } } int main() { #ifdef DMC freopen("DMC.txt","r",stdin); #endif cin >> n >> m; int now = 0; for (int i = 1; i <= n; i++) { if (i >= (1<<(now+1))) ++now; d[i] = now; } for (int i = 1; i < n; i++) { int x,y,z; scanf("%d%d%d",&x,&y,&z); v[x].push_back(E(y,z)); v[y].push_back(E(x,z)); } dfs1(n/2,n); while (m--) { data2 k = Q.top(); Q.pop(); printf("%d\n",k.va); if (k.l < k.po) { int l = k.l,r = k.po - 1; int len = r - l + 1,va,po; int K = r - (1< v2[k.Root][K].Max[d[len]]) va = v2[k.Root][l].Max[d[len]],po = v2[k.Root][l].pos[d[len]]; else va = v2[k.Root][K].Max[d[len]],po = v2[k.Root][K].pos[d[len]]; Q.push(data2(k.sum,l,r,k.Root,va + k.sum,po)); } if (k.po < k.r) { int l = k.po + 1,r = k.r; int len = r - l + 1,va,po; int K = r - (1< v2[k.Root][K].Max[d[len]]) va = v2[k.Root][l].Max[d[len]],po = v2[k.Root][l].pos[d[len]]; else va = v2[k.Root][K].Max[d[len]],po = v2[k.Root][K].pos[d[len]]; Q.push(data2(k.sum,l,r,k.Root,va + k.sum,po)); } } return 0; }