#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " " << x << endl;
// #define int long long // uważaj
const long long N = 3e4 + 7, mod = 1e9 + 7;
int ograniczenia[N];
int akt[N];
vector<pair<int, int> > G[N];
int n, k;
int ile_ustalonych;
int id_ustalonych;
int dfs(int v, int oj) {
int ret = 0;
if(ile_ustalonych == 0 and oj != 0) ret ++;
for(auto x : G[v]) {
if(x.first == oj) continue;
if(ograniczenia[x.second] == akt[x.second]) continue;
akt[x.second] ++;
if(akt[x.second] == ograniczenia[x.second] and x.second >= id_ustalonych) ile_ustalonych--;
ret += dfs(x.first, v);
if(akt[x.second] == ograniczenia[x.second] and x.second >= id_ustalonych) ile_ustalonych++;
akt[x.second] --;
}
return ret;
}
int test() {
int ret = 0;
for(int i = 1; i <= n; i++) {
ret += dfs(i, 0);
// int zm = dfs(i, 0);
}
return ret / 2;
}
void solve() {
cin >> n >> k;
for(int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
ograniczenia[c] ++;
}
id_ustalonych = 1e5;
for(int i = n; i > 0; i--) {
while(ograniczenia[i] > 0) {
ograniczenia[i] --;
int zm = test();
if(zm < k) {
ograniczenia[i] ++;
ile_ustalonych ++;
k -= zm;
break;
}
}
id_ustalonych = i;
}
long long ans = 0;
long long pot = 1;
for(int i = 1; i <= n; i++) {
pot *= n;
pot %= mod;
ans += ograniczenia[i] * pot;
ans %= mod;
}
cout << ans << endl;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int test = 1;
while(test--){
solve();
}
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 | #include <bits/stdc++.h> using namespace std; #define debug(x) cerr << #x << " " << x << endl; // #define int long long // uważaj const long long N = 3e4 + 7, mod = 1e9 + 7; int ograniczenia[N]; int akt[N]; vector<pair<int, int> > G[N]; int n, k; int ile_ustalonych; int id_ustalonych; int dfs(int v, int oj) { int ret = 0; if(ile_ustalonych == 0 and oj != 0) ret ++; for(auto x : G[v]) { if(x.first == oj) continue; if(ograniczenia[x.second] == akt[x.second]) continue; akt[x.second] ++; if(akt[x.second] == ograniczenia[x.second] and x.second >= id_ustalonych) ile_ustalonych--; ret += dfs(x.first, v); if(akt[x.second] == ograniczenia[x.second] and x.second >= id_ustalonych) ile_ustalonych++; akt[x.second] --; } return ret; } int test() { int ret = 0; for(int i = 1; i <= n; i++) { ret += dfs(i, 0); // int zm = dfs(i, 0); } return ret / 2; } void solve() { cin >> n >> k; for(int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); ograniczenia[c] ++; } id_ustalonych = 1e5; for(int i = n; i > 0; i--) { while(ograniczenia[i] > 0) { ograniczenia[i] --; int zm = test(); if(zm < k) { ograniczenia[i] ++; ile_ustalonych ++; k -= zm; break; } } id_ustalonych = i; } long long ans = 0; long long pot = 1; for(int i = 1; i <= n; i++) { pot *= n; pot %= mod; ans += ograniczenia[i] * pot; ans %= mod; } cout << ans << endl; } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int test = 1; while(test--){ solve(); } return 0; } |
English