The data range is n:20w, k100w. Point division, we only need to consider the current tree root scheme. K Max is only 100w, directly open array cnt[x] represents the minimum number of sides to the current root distance of x, and then you can DFS and update the CNT arrays and answers on the root subtree.
------------------------------------------------------------------------------------------
#include <bits/stdc++.h>using namespace std;typedef pair<int, int> PII;const int MAXN = 200009;const int maxk = 1000009;const int INF = 0X3F3F3F3F;inline int Read () {int ret = 0;char C = getchar ();For (;!isdigit (c); c = GetChar ());For (; IsDigit (c); c = GetChar ())ret = ret * + C-' 0 ';return ret;}int N, K, SIZE[MAXN], Rt, Best, N, Cnt[maxk], _n, ANS = INF;PII T[MAXN];bool VIS[MAXN];struct Edge {int to, W;edge* Next;} E[MAXN << 1], *pt = E, *HEAD[MAXN]; inline void Add (int u, int v, int w) {pt->to = v; pt->w = w; pt->next = Head[u]; Head[u] = pt++;}inline void Addedge (int u, int v, int w) {Add (U, V, W); Add (V, u, W);}void Dfs (int x, int fa =-1) {size[x] = 1;int mx = 0;For (edge* e = head[x]; e; e = e->next) if (e->to! = FA &&!vis[e->to]) {DFS (e->to, x);Size[x] + = size[e->to];mx = max (mx, size[e->to]); }if (mx = max (mx, n-size[x)) < best) Rt = x, best = mx;}void DFS (int x, int dist, int cnt, int fa) {if (Dist > K) return;ans = min (ans, cnt + cnt[k-dist]);t[_n++] = Make_pair (dist, cnt++);For (edge* e = head[x]; e; e = e->next) if (e->to! = FA &&!vis[e->to])DFS (e->to, dist + e->w, CNT, x);}void Solve (int x) {Best = INF; dfs (x); x = Rt;int p = _n = 0;For (edge* e = head[x]; e; e = e->next) if (!vis[e->to]) {DFS (E->to, E->w, 1, x);for (int i = p; i < _n; i++)Cnt[t[i].first] = min (Cnt[t[i].first], t[i].second);p = _n; }for (int i = 0; i < _n; i++) Cnt[t[i].first] = INF; Cnt[0] = 0;vis[x] = true;For (edge* e = head[x]; e; e = e->next) if (!vis[e->to]) {n = size[e->to];solve (e->to); }}void init () {N = read (); K = Read ();for (int i = 1; i < N; i++) {int u = Read (), V = Read (), w = read ();Addedge (U, V, W); }}int main () {init ();n = n;memset (CNT, INF, sizeof CNT); Cnt[0] = 0;solve (0);printf ("%d\n", ANS! = INF? ANS:-1);return 0;}
------------------------------------------------------------------------------------------
2599: [Ioi2011]race time limit: Sec Memory Limit: MB
Submit: 1806 Solved: 538
[Submit] [Status] [Discuss]
Description
To a tree, each side of the right. Find a path, weights and equals K, and the number of edges is the smallest.
Input
First row of two integers n, k
Second.. n rows three integers per line represents the ends and weights of a non-forward edge (note numbering starts with 0)
Output
An integer representing the minimum number of edges if there is no such path output-1
Sample Input4 3
0 1 1
1 2 2
1 3 4
Sample Output2
HINT
Source
Bzoj 2599: [Ioi2011]race (Point Division)