#include <bits/stdc++.h> using namespace std; using vi=vector<int>; using ll=long long; #define pb push_back #define fst first #define snd second using vll=vector<ll>; using Pii=pair<int,int>; using Pll=pair<ll,ll>; using vpii=vector<Pii>; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define sim template <class c #define mor > muu & operator<<( #define ris return *this #define R22(r) sim> typename enable_if<1 r sizeof(dud<c>(0)),muu&>::type operator<<(c g) { sim> struct rge {c b, e;}; sim> rge<c> range(c i, c j) {return rge<c>{i, j};} sim> auto dud(c*r)->decltype(cerr << *r); sim> char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() {cerr << a.str() << endl;} R22(<) a << boolalpha << g; ris;} R22(==) ris << range(begin(g), end(g));} sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } sim, class m mor pair<m,c> r) {ris << "(" << r.first << ", " << r.second << ")";} #else sim mor const c &){ris;} #endif muu & operator()(){ris;} }; #define imie(r) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": " ) const int maxn = 1e5; vector<Pll> E[maxn]; bool wyjebane[maxn]; int w_poddrzewie[maxn]; int w_dzieciach[maxn]; bool visited[maxn]; void dfs(int x, vector<int>& wierzcholki) { visited[x] = true; w_poddrzewie[x] = 1; w_dzieciach[x] = 0; wierzcholki.push_back(x); for(Pll p : E[x]) { int y = p.first; if(!wyjebane[y] && !visited[y]) { dfs(y, wierzcholki); w_poddrzewie[x] += w_poddrzewie[y]; w_dzieciach[x] = max(w_dzieciach[x], w_poddrzewie[y]); } } } pair<int, vi> znajdz_centroid(int x) { vi W; dfs(x, W); for(int v : W) visited[v] = false; int total_size = w_poddrzewie[x]; int sz = W.size(); for(int v : W) { if(w_dzieciach[v] <= sz/2 && total_size - w_poddrzewie[v] <= sz/2) return {v, W}; } assert(false); //return -1; } ll limit; void dfs2(int x, ll acc, vll& paths) { paths.push_back(acc); assert(acc != 0); visited[x] = true; for(Pll p : E[x]) if(!wyjebane[p.first] && !visited[p.first]) dfs2(p.first, acc + p.second, paths); } int jebaj_sciezki_single(vector<ll> A, vector<ll> B) { int res = 0; sort(A.begin(), A.end()); sort(B.begin(), B.end()); int idx = (int)B.size() - 1; for(ll x : A) { while(idx >= 0 && x + B[idx] > limit) --idx; res += idx + 1; } return res; } int jebaj_sciezki(vector<vll> sc) { if((int)sc.size() < 2) return 0; int sz = sc.size(); vector<vll> A, B; vll a, b; for(int i = 0; i < sz/2; ++i) { A.push_back(sc[i]); for(ll v : sc[i]) a.push_back(v); } for(int i = sz/2; i < sz; ++i) { B.push_back(sc[i]); for(ll v : sc[i]) b.push_back(v); } return jebaj_sciezki(A) + jebaj_sciezki(B) + jebaj_sciezki_single(a, b); } int jebaj(int x) { int res = 0; auto P = znajdz_centroid(x); vi wierzcholki = P.second; x = P.first; wyjebane[x] = true; vector<vll> sciezki = {{0}}; for(Pll p : E[x]) if(!wyjebane[p.first]) { vll pat; dfs2(p.first, p.second, pat); res += jebaj(p.first); sciezki.push_back(pat); } debug() << imie(x); return res + jebaj_sciezki(sciezki); } int n, k; const int D = 16; const int maska = (1 << D) - 1; int main() { scanf("%d%d", &n, &k); int licz[4] = {0,0,0,0}; for(int i = 1; i < n; ++i) { int a, b, p; scanf("%d%d%d", &a, &b, &p); --a; --b; --p; ll wyk = 1ll << (D * p); debug() << imie(wyk); E[a].push_back({b, wyk}); E[b].push_back({a, wyk}); licz[p]++; } ll b = -1; ll e = n * (1ll << D * 3); while(e - b > 1) { ll d = (b + e) / 2; limit = d; debug() << "KURWA"; for(int i = 0; i < n; ++i) wyjebane[i] = visited[i] = false; if(jebaj(0) < k) b = d; else e = d; //~ for(int i = 0; i < n; ++i) //~ debug() << imie(wyjebane[i]); } ll mn = n; ll res = 0; while(e > 0) { res += mn * (e & maska); e >>= D; mn = n * mn; } const ll mod = 1e9 + 7; printf("%lld\n", res % mod); }
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 | #include <bits/stdc++.h> using namespace std; using vi=vector<int>; using ll=long long; #define pb push_back #define fst first #define snd second using vll=vector<ll>; using Pii=pair<int,int>; using Pll=pair<ll,ll>; using vpii=vector<Pii>; #ifndef LOCAL #pragma GCC optimize("O3") #endif #define sim template <class c #define mor > muu & operator<<( #define ris return *this #define R22(r) sim> typename enable_if<1 r sizeof(dud<c>(0)),muu&>::type operator<<(c g) { sim> struct rge {c b, e;}; sim> rge<c> range(c i, c j) {return rge<c>{i, j};} sim> auto dud(c*r)->decltype(cerr << *r); sim> char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() {cerr << a.str() << endl;} R22(<) a << boolalpha << g; ris;} R22(==) ris << range(begin(g), end(g));} sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } sim, class m mor pair<m,c> r) {ris << "(" << r.first << ", " << r.second << ")";} #else sim mor const c &){ris;} #endif muu & operator()(){ris;} }; #define imie(r) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": " ) const int maxn = 1e5; vector<Pll> E[maxn]; bool wyjebane[maxn]; int w_poddrzewie[maxn]; int w_dzieciach[maxn]; bool visited[maxn]; void dfs(int x, vector<int>& wierzcholki) { visited[x] = true; w_poddrzewie[x] = 1; w_dzieciach[x] = 0; wierzcholki.push_back(x); for(Pll p : E[x]) { int y = p.first; if(!wyjebane[y] && !visited[y]) { dfs(y, wierzcholki); w_poddrzewie[x] += w_poddrzewie[y]; w_dzieciach[x] = max(w_dzieciach[x], w_poddrzewie[y]); } } } pair<int, vi> znajdz_centroid(int x) { vi W; dfs(x, W); for(int v : W) visited[v] = false; int total_size = w_poddrzewie[x]; int sz = W.size(); for(int v : W) { if(w_dzieciach[v] <= sz/2 && total_size - w_poddrzewie[v] <= sz/2) return {v, W}; } assert(false); //return -1; } ll limit; void dfs2(int x, ll acc, vll& paths) { paths.push_back(acc); assert(acc != 0); visited[x] = true; for(Pll p : E[x]) if(!wyjebane[p.first] && !visited[p.first]) dfs2(p.first, acc + p.second, paths); } int jebaj_sciezki_single(vector<ll> A, vector<ll> B) { int res = 0; sort(A.begin(), A.end()); sort(B.begin(), B.end()); int idx = (int)B.size() - 1; for(ll x : A) { while(idx >= 0 && x + B[idx] > limit) --idx; res += idx + 1; } return res; } int jebaj_sciezki(vector<vll> sc) { if((int)sc.size() < 2) return 0; int sz = sc.size(); vector<vll> A, B; vll a, b; for(int i = 0; i < sz/2; ++i) { A.push_back(sc[i]); for(ll v : sc[i]) a.push_back(v); } for(int i = sz/2; i < sz; ++i) { B.push_back(sc[i]); for(ll v : sc[i]) b.push_back(v); } return jebaj_sciezki(A) + jebaj_sciezki(B) + jebaj_sciezki_single(a, b); } int jebaj(int x) { int res = 0; auto P = znajdz_centroid(x); vi wierzcholki = P.second; x = P.first; wyjebane[x] = true; vector<vll> sciezki = {{0}}; for(Pll p : E[x]) if(!wyjebane[p.first]) { vll pat; dfs2(p.first, p.second, pat); res += jebaj(p.first); sciezki.push_back(pat); } debug() << imie(x); return res + jebaj_sciezki(sciezki); } int n, k; const int D = 16; const int maska = (1 << D) - 1; int main() { scanf("%d%d", &n, &k); int licz[4] = {0,0,0,0}; for(int i = 1; i < n; ++i) { int a, b, p; scanf("%d%d%d", &a, &b, &p); --a; --b; --p; ll wyk = 1ll << (D * p); debug() << imie(wyk); E[a].push_back({b, wyk}); E[b].push_back({a, wyk}); licz[p]++; } ll b = -1; ll e = n * (1ll << D * 3); while(e - b > 1) { ll d = (b + e) / 2; limit = d; debug() << "KURWA"; for(int i = 0; i < n; ++i) wyjebane[i] = visited[i] = false; if(jebaj(0) < k) b = d; else e = d; //~ for(int i = 0; i < n; ++i) //~ debug() << imie(wyjebane[i]); } ll mn = n; ll res = 0; while(e > 0) { res += mn * (e & maska); e >>= D; mn = n * mn; } const ll mod = 1e9 + 7; printf("%lld\n", res % mod); } |