#include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; const long long HMOD = 1000000LL * 1000000LL * 1000000LL + 3; struct edge { int u, v, p; }; long long randomHash() { long long res = 0; res ^= rand(); res <<= 32; res ^= rand(); return res % HMOD; } void addHash(long long &x, long long val) { x += val; if (x >= HMOD) { x -= HMOD; } } void subHash(long long &x, long long val) { x -= val; if (x < 0) { x += HMOD; } } struct Centroid { const int MAXLG = 20; vector <vector<int>> edges; vector <bool> taken; vector <int> size; vector <int> jump; // output: parent vector <int> depth; // output: layer vector <vector<int>> dist, from, parent; // output: dist from ancestor at given depth, and first on path Centroid(int n): edges(n), taken(n, false), size(n), jump(n), depth(n), dist(MAXLG, vector <int> (n)), from(MAXLG, vector <int> (n)), parent(MAXLG, vector <int> (n, -1)) {} void addEdge(int u, int v) { edges[u].push_back(v); edges[v].push_back(u); } void updateSize(int u) { taken[u] = size[u] = 1; for (int v : edges[u]) if (!taken[v]) { updateSize(v); size[u] += size[v]; } taken[u] = 0; } pair <int,int> centroid(int u, int n) { taken[u] = 1; pair <int,int> r = {n - size[u], u}; for (int v : edges[u]) if (!taken[v]) r.first = max(r.first, size[v]); for (int v : edges[u]) if (!taken[v]) r = min(r, centroid(v, n)); taken[u] = 0; return r; } void fillDist(int u, int d, int par, int layer) { taken[u] = 1; dist[layer][u] = d; from[layer][u] = par; if (par == -1) { par = u; } for (int v : edges[u]) if (!taken[v]) { parent[layer][v] = u; fillDist(v, d + 1, par, layer); } taken[u] = 0; } void decompose(int u, int p, int layer) { updateSize(u); int n = size[u]; u = centroid(u, n).second; jump[u] = p; depth[u] = layer; fillDist(u, 0, -1, layer); taken[u] = 1; for (int v : edges[u]) if (!taken[v]) decompose(v, u, layer+1); } void decompose() { decompose(0, 0, 0); } }; vector <int> ubs; void dfs(int u, long long h, vector <vector<pair<int,int>>> &g, function <long long(int)> &getHash, vector <long long> &seen) { seen.push_back(h); for (auto &e : g[u]) { int v = e.first; if (!ubs[e.second]) continue; ubs[e.second]--; long long hHere = getHash(e.second); addHash(hHere, h); dfs(v, hHere, g, getHash, seen); ubs[e.second]++; } } unordered_map <long long,int> cnt; int countRootPaths(int u, vector <vector<pair<int,int>>> &g, int i, vector <long long> &pHash, long long targetHash) { function <long long(int)> getHash = [&](int p) { return p >= i ? pHash[p] : 0; }; int ans = 0; cnt.clear(); cnt[0] = 1; for (auto &e : g[u]) { int v = e.first; if (!ubs[e.second]) continue; ubs[e.second]--; long long h = getHash(e.second); vector <long long> seen; dfs(v, h, g, getHash, seen); for (long long &hHere : seen) { long long target = targetHash; subHash(target, hHere); auto f = cnt.find(target); if (f != cnt.end()) { ans += f->second; } } for (long long &hHere : seen) { cnt[hHere]++; } ubs[e.second]++; } return ans; } int solve(int n, int k, vector <edge> &edges) { vector <vector<pair<int,int>>> edgesPerCost(n); map <pair<int,int>,int> costForEdge; Centroid centroid(n); for (auto &e : edges) { edgesPerCost[e.p].push_back({e.u, e.v}); costForEdge[{e.u, e.v}] = e.p; costForEdge[{e.v, e.u}] = e.p; centroid.addEdge(e.u, e.v); } centroid.decompose(); vector <vector<int>> forRoot(n); for (int v = 0; v < n; v++) { int anc = v; for (int level = centroid.depth[v]; level >= 0; level--) { forRoot[anc].push_back(v); anc = centroid.jump[anc]; } } vector <vector<vector<pair<int,int>>>> tree(n); vector <int> treeRootId(n); vector <vector<int>> rootsWithCost(n); for (int root = 0; root < n; root++) { auto verts = forRoot[root]; sort(verts.begin(), verts.end()); int m = verts.size(); tree[root].resize(m); auto mapDown = [&](int u) { return lower_bound(verts.begin(), verts.end(), u) - verts.begin(); }; for (int v : verts) if (v != root) { int par = centroid.parent[centroid.depth[root]][v]; int cost = costForEdge[{par, v}]; tree[root][mapDown(par)].push_back({mapDown(v), cost}); rootsWithCost[cost].push_back(root); } treeRootId[root] = mapDown(root); } vector <int> power(n, n); for (int i = 1; i < n; i++) { power[i] = ((long long) power[i - 1] * n) % MOD; } vector <long long> pHash(n); for (auto &h : pHash) { h = randomHash(); } int ans = 0; long long expHash = 0; vector <int> rootsAlive; for (int root = 0; root < n; root++) if (forRoot[root].size() > 1) { rootsAlive.push_back(root); } ubs.resize(n, n); for (int i = n - 1; i >= 0; i--) if (!edgesPerCost[i].empty()) { vector <int> inRoot(n, 0); for (int root : rootsWithCost[i]) { inRoot[root]++; } for (int m = 0; m < edgesPerCost[i].size(); m++) { int cntHere = 0; vector <int> rootsAliveHere; for (int root : rootsAlive) if (inRoot[root] >= m) { int res = countRootPaths(treeRootId[root], tree[root], i, pHash, expHash); if (res > 0) { cntHere += res; rootsAliveHere.push_back(root); } } if (cntHere >= k) { rootsAlive = rootsAliveHere; ubs[i] = m; break; } else { k -= cntHere; } ans = (ans + power[i]) % MOD; addHash(expHash, pHash[i]); } } return ans; } int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector <edge> edges(n - 1); for (auto &e : edges) { cin >> e.u >> e.v >> e.p; e.u--; e.v--; e.p--; } cout << solve(n, k, edges); return 0; }
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | #include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; const long long HMOD = 1000000LL * 1000000LL * 1000000LL + 3; struct edge { int u, v, p; }; long long randomHash() { long long res = 0; res ^= rand(); res <<= 32; res ^= rand(); return res % HMOD; } void addHash(long long &x, long long val) { x += val; if (x >= HMOD) { x -= HMOD; } } void subHash(long long &x, long long val) { x -= val; if (x < 0) { x += HMOD; } } struct Centroid { const int MAXLG = 20; vector <vector<int>> edges; vector <bool> taken; vector <int> size; vector <int> jump; // output: parent vector <int> depth; // output: layer vector <vector<int>> dist, from, parent; // output: dist from ancestor at given depth, and first on path Centroid(int n): edges(n), taken(n, false), size(n), jump(n), depth(n), dist(MAXLG, vector <int> (n)), from(MAXLG, vector <int> (n)), parent(MAXLG, vector <int> (n, -1)) {} void addEdge(int u, int v) { edges[u].push_back(v); edges[v].push_back(u); } void updateSize(int u) { taken[u] = size[u] = 1; for (int v : edges[u]) if (!taken[v]) { updateSize(v); size[u] += size[v]; } taken[u] = 0; } pair <int,int> centroid(int u, int n) { taken[u] = 1; pair <int,int> r = {n - size[u], u}; for (int v : edges[u]) if (!taken[v]) r.first = max(r.first, size[v]); for (int v : edges[u]) if (!taken[v]) r = min(r, centroid(v, n)); taken[u] = 0; return r; } void fillDist(int u, int d, int par, int layer) { taken[u] = 1; dist[layer][u] = d; from[layer][u] = par; if (par == -1) { par = u; } for (int v : edges[u]) if (!taken[v]) { parent[layer][v] = u; fillDist(v, d + 1, par, layer); } taken[u] = 0; } void decompose(int u, int p, int layer) { updateSize(u); int n = size[u]; u = centroid(u, n).second; jump[u] = p; depth[u] = layer; fillDist(u, 0, -1, layer); taken[u] = 1; for (int v : edges[u]) if (!taken[v]) decompose(v, u, layer+1); } void decompose() { decompose(0, 0, 0); } }; vector <int> ubs; void dfs(int u, long long h, vector <vector<pair<int,int>>> &g, function <long long(int)> &getHash, vector <long long> &seen) { seen.push_back(h); for (auto &e : g[u]) { int v = e.first; if (!ubs[e.second]) continue; ubs[e.second]--; long long hHere = getHash(e.second); addHash(hHere, h); dfs(v, hHere, g, getHash, seen); ubs[e.second]++; } } unordered_map <long long,int> cnt; int countRootPaths(int u, vector <vector<pair<int,int>>> &g, int i, vector <long long> &pHash, long long targetHash) { function <long long(int)> getHash = [&](int p) { return p >= i ? pHash[p] : 0; }; int ans = 0; cnt.clear(); cnt[0] = 1; for (auto &e : g[u]) { int v = e.first; if (!ubs[e.second]) continue; ubs[e.second]--; long long h = getHash(e.second); vector <long long> seen; dfs(v, h, g, getHash, seen); for (long long &hHere : seen) { long long target = targetHash; subHash(target, hHere); auto f = cnt.find(target); if (f != cnt.end()) { ans += f->second; } } for (long long &hHere : seen) { cnt[hHere]++; } ubs[e.second]++; } return ans; } int solve(int n, int k, vector <edge> &edges) { vector <vector<pair<int,int>>> edgesPerCost(n); map <pair<int,int>,int> costForEdge; Centroid centroid(n); for (auto &e : edges) { edgesPerCost[e.p].push_back({e.u, e.v}); costForEdge[{e.u, e.v}] = e.p; costForEdge[{e.v, e.u}] = e.p; centroid.addEdge(e.u, e.v); } centroid.decompose(); vector <vector<int>> forRoot(n); for (int v = 0; v < n; v++) { int anc = v; for (int level = centroid.depth[v]; level >= 0; level--) { forRoot[anc].push_back(v); anc = centroid.jump[anc]; } } vector <vector<vector<pair<int,int>>>> tree(n); vector <int> treeRootId(n); vector <vector<int>> rootsWithCost(n); for (int root = 0; root < n; root++) { auto verts = forRoot[root]; sort(verts.begin(), verts.end()); int m = verts.size(); tree[root].resize(m); auto mapDown = [&](int u) { return lower_bound(verts.begin(), verts.end(), u) - verts.begin(); }; for (int v : verts) if (v != root) { int par = centroid.parent[centroid.depth[root]][v]; int cost = costForEdge[{par, v}]; tree[root][mapDown(par)].push_back({mapDown(v), cost}); rootsWithCost[cost].push_back(root); } treeRootId[root] = mapDown(root); } vector <int> power(n, n); for (int i = 1; i < n; i++) { power[i] = ((long long) power[i - 1] * n) % MOD; } vector <long long> pHash(n); for (auto &h : pHash) { h = randomHash(); } int ans = 0; long long expHash = 0; vector <int> rootsAlive; for (int root = 0; root < n; root++) if (forRoot[root].size() > 1) { rootsAlive.push_back(root); } ubs.resize(n, n); for (int i = n - 1; i >= 0; i--) if (!edgesPerCost[i].empty()) { vector <int> inRoot(n, 0); for (int root : rootsWithCost[i]) { inRoot[root]++; } for (int m = 0; m < edgesPerCost[i].size(); m++) { int cntHere = 0; vector <int> rootsAliveHere; for (int root : rootsAlive) if (inRoot[root] >= m) { int res = countRootPaths(treeRootId[root], tree[root], i, pHash, expHash); if (res > 0) { cntHere += res; rootsAliveHere.push_back(root); } } if (cntHere >= k) { rootsAlive = rootsAliveHere; ubs[i] = m; break; } else { k -= cntHere; } ans = (ans + power[i]) % MOD; addHash(expHash, pHash[i]); } } return ans; } int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector <edge> edges(n - 1); for (auto &e : edges) { cin >> e.u >> e.v >> e.p; e.u--; e.v--; e.p--; } cout << solve(n, k, edges); return 0; } |