#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define DPRINT(x) cout << #x << ": " << x << endl #define BOOST ios_base::sync_with_stdio(false); cin.tie(0) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " // ゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴ const int maxn = 25042; int ans[5]; int curr[5]; int cnt[maxn]; VPII gr[maxn]; vector<pair<int, LL> > gr2[maxn]; LL pows[maxn]; void dfs(int a, int o, int p, bool nodo = false) { // cout << "dfs " << a << endl; bool ok = true; FORD(i, 4, p+1) if(curr[i] != ans[i]) { ok = false; break; } if(ok) { // cout << "in " << a << " popycha " << curr[p] << endl; cnt[curr[p]]++; } for(auto i : gr[a]) if(i.ST != o) { curr[i.ND]++; dfs(i.ST, a, p); curr[i.ND]--; } } VI curr2; multiset<VI> xxx; int mp; void dfs2(int a, int o) { xxx.insert(curr2); for(auto i : gr[a]) if(i.ST != o) { curr2[mp-i.ND]++; dfs2(i.ST, a); curr2[mp-i.ND]--; } } int main() { BOOST; int n, k; cin >> n >> k; k *= 2; k += n; FOR(i, 1, n-1) { int a, b, p; cin >> a >> b >> p; gr[a].PB({b, p}); gr[b].PB({a, p}); mp = max(mp, p); } if(mp <= 4) { FORD(p, 4, 1) { FOR(i, 0, n+1) cnt[i] = 0; FOR(i, 1, n) dfs(i, -1, p, true); /* FOR(i, 0, 5) cout << cnt[i] << ' '; cout << endl; */ int c = 0; int a = -1; while(c + cnt[a+1] < k) { c += cnt[a+1]; a++; } k -= c; ans[p] = a + 1; //cout << ans[p] << " ans" << endl; } /* cout << "ans[]: "; FOR(i, 1, 4) cout << ans[i] << ' '; cout << endl; */ LL gans = 0; LL x = n; FOR(p, 1, 4) { gans = (gans + x * ans[p]) % MOD; x = (x * n) % MOD; } cout << gans; } else { curr2.resize(mp+1); FOR(i, 1, n) dfs2(i, -1); /* for(auto i : xxx) { for(auto j : i) cout << j << ' '; cout << endl; } */ auto it = xxx.begin(); FOR(i, 1, k-1) it++; auto v = *it; LL ans = 0; LL x = n; FOR(i, 1, mp) { // cout << i << ' '; ans = (ans + x * v[mp-i]) % MOD; x = (x * n) % MOD; } cout << ans; } }
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define DPRINT(x) cout << #x << ": " << x << endl #define BOOST ios_base::sync_with_stdio(false); cin.tie(0) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " // ゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴゴ const int maxn = 25042; int ans[5]; int curr[5]; int cnt[maxn]; VPII gr[maxn]; vector<pair<int, LL> > gr2[maxn]; LL pows[maxn]; void dfs(int a, int o, int p, bool nodo = false) { // cout << "dfs " << a << endl; bool ok = true; FORD(i, 4, p+1) if(curr[i] != ans[i]) { ok = false; break; } if(ok) { // cout << "in " << a << " popycha " << curr[p] << endl; cnt[curr[p]]++; } for(auto i : gr[a]) if(i.ST != o) { curr[i.ND]++; dfs(i.ST, a, p); curr[i.ND]--; } } VI curr2; multiset<VI> xxx; int mp; void dfs2(int a, int o) { xxx.insert(curr2); for(auto i : gr[a]) if(i.ST != o) { curr2[mp-i.ND]++; dfs2(i.ST, a); curr2[mp-i.ND]--; } } int main() { BOOST; int n, k; cin >> n >> k; k *= 2; k += n; FOR(i, 1, n-1) { int a, b, p; cin >> a >> b >> p; gr[a].PB({b, p}); gr[b].PB({a, p}); mp = max(mp, p); } if(mp <= 4) { FORD(p, 4, 1) { FOR(i, 0, n+1) cnt[i] = 0; FOR(i, 1, n) dfs(i, -1, p, true); /* FOR(i, 0, 5) cout << cnt[i] << ' '; cout << endl; */ int c = 0; int a = -1; while(c + cnt[a+1] < k) { c += cnt[a+1]; a++; } k -= c; ans[p] = a + 1; //cout << ans[p] << " ans" << endl; } /* cout << "ans[]: "; FOR(i, 1, 4) cout << ans[i] << ' '; cout << endl; */ LL gans = 0; LL x = n; FOR(p, 1, 4) { gans = (gans + x * ans[p]) % MOD; x = (x * n) % MOD; } cout << gans; } else { curr2.resize(mp+1); FOR(i, 1, n) dfs2(i, -1); /* for(auto i : xxx) { for(auto j : i) cout << j << ' '; cout << endl; } */ auto it = xxx.begin(); FOR(i, 1, k-1) it++; auto v = *it; LL ans = 0; LL x = n; FOR(i, 1, mp) { // cout << i << ' '; ans = (ans + x * v[mp-i]) % MOD; x = (x * n) % MOD; } cout << ans; } } |