#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define vout(v) for(auto u : v){cout<<u<<' ';} cout<<'\n'
#define dout(a) cout<<a<<' '<<#a<<'\n'
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<'\n'
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
const int leg = 1e9 + 7;
const int mod = 998244353;
using namespace std;
const int mxn = 1e5 + 5;
const int lg = 11;
int sz[mxn];
int par[mxn];
int find(int x){
while(par[x] != x)x = par[x];
return x;
}
void unite(int a, int b){
//out3(a,b,small);
a = find(a);
b = find(b);
if(sz[a] < sz[b])swap(a,b);
sz[a] += sz[b];
par[b] = a;
}
int solve(int lef, int rig, int mxl){
if(lef > rig)swap(lef, rig);
int add = (min(mxl, lef) + 1) * (min(mxl, lef) + 2) / 2;
if(mxl > lef){
int rem = min(rig - lef, mxl - lef);
add += rem * (lef + 1);
}
if(mxl > rig){
int rem = mxl - rig;
add += (lef + lef - rem + 1) * rem / 2;
}
return add;
}
vpii adj[mxn];
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin(".in");
//ofstream cout(".out");
//dout(solve(3,5,9));
in2(n,k);
vi v(n-1);
f0r(i,n-1){
in3(a,b,w);
a--; b--;
v[min(a,b)] = w;
adj[a].pb(mp(b,w));
adj[b].pb(mp(a,w));
}
if(n<=1000){
int anss = 0;
queue<int>q;
q.push(0);
vi dep(n,-1);
dep[0] = 0;
vpii par(n);
par[0] = mp(-1, -1);
while(!q.empty()){
int node = q.front();
q.pop();
for(auto you : adj[node]){
int u = you.first;
if(dep[u] == -1){
dep[u] = dep[node] + 1;
par[u] = mp(node, you.second);
q.push(u);
}
}
}
pii jmp[n][lg];
f0r(i,n){
jmp[i][0] = par[i];
}
FOR(i, 1, lg){
f0r(j,n){
if(jmp[j][i-1].first == -1)jmp[j][i] = mp(-1,-1);
else{
int a = jmp[jmp[j][i-1].first][i-1].first;
int b = max(jmp[j][i-1].second, jmp[jmp[j][i-1].first][i-1].second);
jmp[j][i] = mp(a,b);
}
}
}
f0r(i,n){
f0r(j,n){
if(i != j){
int a = i;
int b = j;
if(dep[a] < dep[b])swap(a,b);
int thing = dep[a] - dep[b];
int ans = -1;
int len = 0;
f0r(k,lg){
if((1 << k) & thing){
ans = max(ans, jmp[a][k].second);
a = jmp[a][k].first;
len += (1 << k);
}
}
for(int k = lg - 1; k>=0; k--){
if(jmp[a][k].first != jmp[b][k].first){
ans = max(ans, jmp[a][k].second);
ans = max(ans, jmp[b][k].second);
a = jmp[a][k].first;
b = jmp[b][k].first;
len += (1 << (k + 1));
}
}
if(a != b){
ans = max(ans, par[a].second);
ans = max(ans, par[b].second);
len += 2;
}
if(ans - len >= k)anss++;
}
}
}
out(anss);
}
else{
vpii w;
f0r(i,n-1){
w.pb(mp(v[i],i));
}
sort(all(w));
f0r(i,n){
sz[i] = 1;
par[i] = i;
}
int ans = 0;
f0r(i, n-1){
int gw = w[i].first;
int en = w[i].second;
int mxl = gw - k - 1;
if(mxl >= 0){
int lef = 0;
lef = sz[find(en)] - 1;
int rig = 0;
rig = sz[find(en + 1)] - 1;
//out3(mxl, lef, rig);
int sol = solve(lef, rig, min(lef + rig, mxl));
ans += sol;
}
unite(en, en+1);
//dout(ans);
}
out(ans * 2);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |