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 137 138 139 140 141
| #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #define MAXN (200000 + 5) #define INF (0x7fffffff) #define cint const int #define rint register int #define pii pair<int, int> #define max(a, b) ((a) > (b) ? (a) : (b)) #pragma GCC optimize(3)
using namespace std; int to[MAXN << 1], next[MAXN << 1], cost[MAXN << 1]; int g[MAXN], cntb, n, k, ans = -1, totdis, maxside[MAXN], size[MAXN], cnt[MAXN], dis0; pii dis[MAXN]; bool vis[MAXN]; inline void adn(cint fro, cint t, cint co) { next[++cntb] = g[fro]; to[cntb] = t; cost[cntb] = co; g[fro] = cntb; } inline char gc(){ static char BUFF[1000000],*S=BUFF,*T=BUFF; return S==T&&(T=(S=BUFF)+fread(BUFF,1,1000000,stdin),S==T)?EOF:*S++; } inline int read() { int x=0,c=1; char ch=' '; while((ch>'9'||ch<'0')&&ch!='-')ch=gc(); while(ch=='-')c*=-1,ch=gc(); while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=gc(); return x*c; } inline void cal(cint, cint, cint, cint); void dfs_dis(cint, cint, cint, cint); inline int get_root(cint); int dfs_root(cint, cint, cint); void init_size(cint, cint); void dfs(cint); int main() {
n = read(), k = read(); for (rint i = 1; i < n; ++i) {
rint srx = read(), sry = read(), srz = read();
++srx, ++sry; adn(srx, sry, srz); adn(sry, srx, srz); } dfs(1); for (rint i = 0; i < n && ans == -1; ++i) if (cnt[i] > 0) ans = i; printf("%d", ans); return 0; } void dfs(cint dq) { cint root = get_root(dq); vis[root] = true; cal(root, 0, 1, 0); for (rint i = g[root]; i; i = next[i]) if (!vis[to[i]]) { cal(to[i], cost[i], -1, 2); dfs(to[i]); } } inline int get_root(cint dq) { init_size(dq, dq); return dfs_root(dq, dq, dq); } void init_size(cint dq, cint fa) { maxside[dq] = 0; size[dq] = 1; for (rint i = g[dq]; i; i = next[i]) if (!vis[to[i]] && to[i] != fa) { init_size(to[i], dq); maxside[dq] = max(maxside[dq], size[to[i]]); size[dq] += size[to[i]]; } } int dfs_root(cint dq, cint fa, cint root) { rint re, minmaxside = n; for (rint i = g[dq]; i; i = next[i]) if (!vis[to[i]] && to[i] != fa) { cint t = dfs_root(to[i], dq, root); if (maxside[t] < minmaxside) re = t, minmaxside = maxside[t]; } maxside[dq] = max(maxside[dq], size[root] - size[dq]); if (maxside[dq] < minmaxside) re = dq; return re; } void dfs_dis(cint dq, cint fa, cint dqdis, cint way) { dis[++dis0] = make_pair(dqdis, way); for (rint i = g[dq]; i; i = next[i]) if (to[i] != fa && !vis[to[i]]) dfs_dis(to[i], dq, dqdis + cost[i], way + 1); } inline void cal(cint dq, cint fir, cint zh, cint wa) { dis0 = 0; dfs_dis(dq, dq, fir, 0); sort(dis + 1, dis + dis0 + 1); rint j = dis0; for (rint i = 1; i < j; i++) { while (dis[i].first + dis[j].first > k && i < j) --j; rint last = j; while (dis[i].first + dis[last].first == k && i < last) { cnt[dis[i].second + dis[last].second + wa] += zh; --last; } } }
|