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
#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned int U;
typedef long long ll;
typedef unsigned long long ull;
const int N=25010,CNT=500005,M=CNT*17,P=1000000007,LIM=150000;
int n,_,i,j,k,x,y,z,ct;ll K;
int root[N],g[N],v[N<<1],w[N<<1],nxt[N<<1],ok[N<<1],ed,son[N],f[N],now,all;
int pool[CNT],cp,q[CNT<<1],cq,cnt,st[N<<1],en[N<<1],base[N<<1];
int pl[CNT],pr[CNT];
int tot,l[M],r[M],sum[M],val[M],p[N];ll sw[M],weight[N];
int LA,LB,RA,RB,MA,MB,ANSA,ANSB;
ll total;
U SX=335634763,SY=873658265,SZ=192849106,SW=746126501;
int ce;
struct E{int l,r;E(){}E(int _l,int _r){l=_l,r=_r;}}e[LIM+5];
inline ull xorshift128(){
  U t=SX^(SX<<11);
  SX=SY;
  SY=SZ;
  SZ=SW;
  return SW=SW^(SW>>19)^t^(t>>8);
}
inline ull myrand(){return (xorshift128()<<32)^xorshift128();}
int ins(int x,int a,int b,int c){
  int y=++tot;
  val[y]=val[x]+1;
  sum[y]=(sum[x]+p[c])%P;
  sw[y]=sw[x]+weight[c];
  if(a==b)return y;
  int mid=(a+b)>>1;
  if(c<=mid)l[y]=ins(l[x],a,mid,c),r[y]=r[x];
  else l[y]=l[x],r[y]=ins(r[x],mid+1,b,c);
  return y;
}
inline int compare(int A,int B,int C,int D){
  //A+B < C+D -1
  //A+B = C+D 0
  //A+B > C+D 1
  if(sw[A]+sw[B]==sw[C]+sw[D])return 0;
  int a=1,b=n,mid;
  while(a<b){
    mid=(a+b)>>1;
    if(sw[r[A]]+sw[r[B]]==sw[r[C]]+sw[r[D]]){
      b=mid;
      A=l[A];
      B=l[B];
      C=l[C];
      D=l[D];
    }else{
      a=mid+1;
      A=r[A];
      B=r[B];
      C=r[C];
      D=r[D];
    }
  }
  return val[A]+val[B]<val[C]+val[D]?-1:1;
}
inline bool cmp(int x,int y){return compare(x,0,y,0)<0;}
inline bool cmpe(const E&a,const E&b){return compare(a.l,a.r,b.l,b.r)<0;}
inline void add(int x,int y,int z){v[++ed]=y;w[ed]=z;nxt[ed]=g[x];g[x]=ed;ok[ed]=1;}
void findroot(int x,int y){
  son[x]=1;f[x]=0;
  for(int i=g[x];i;i=nxt[i])if(ok[i]&&v[i]!=y){
    findroot(v[i],x);
    son[x]+=son[v[i]];
    if(son[v[i]]>f[x])f[x]=son[v[i]];
  }
  if(all-son[x]>f[x])f[x]=all-son[x];
  if(f[x]<f[now])now=x;
}
void dfs1(int x,int y,int z){
  root[x]=z;
  pool[++cp]=z;
  for(int i=g[x];i;i=nxt[i])if(v[i]!=y&&ok[i])dfs1(v[i],x,ins(z,1,n,w[i]));
}
void dfs2(int x,int y){
  q[++cq]=root[x];
  for(int i=g[x];i;i=nxt[i])if(v[i]!=y&&ok[i])dfs2(v[i],x);
}
void solve(int x){
  int i;
  dfs1(x,0,0);
  base[++cnt]=1;
  st[cnt]=cq+1;
  dfs2(x,0);
  en[cnt]=cq;
  if(en[cnt]==st[cnt])cnt--,cq--;
  for(i=g[x];i;i=nxt[i])if(ok[i]){
    base[++cnt]=-1;
    st[cnt]=cq+1;
    dfs2(v[i],x);
    en[cnt]=cq;
    if(en[cnt]==st[cnt])cnt--,cq--;
  }
  for(i=g[x];i;i=nxt[i])if(ok[i]){
    ok[i^1]=0;
    f[0]=all=son[v[i]];
    findroot(v[i],now=0);
    solve(now);
  }
}
inline void dec(int l,int r){
  int i,j;
  for(i=l,j=r;i<=r;i++){
    while(j>i&&compare(MA,MB,q[i],q[j])<0)j--;
    if(j<=i)break;
    total-=j-i;
  }
}
inline void inc(int l,int r){
  int i,j;
  if(total>=K)return;
  for(i=l,j=r;i<=r;i++){
    while(j>i&&compare(MA,MB,q[i],q[j])<0)j--;
    if(j<=i)break;
    total+=j-i;
    if(total>=K)return;
  }
}
int main(){
  scanf("%d%lld",&n,&K);
  for(i=1;i<=n;i++)weight[i]=myrand();
  for(p[0]=i=1;i<=n;i++)p[i]=1LL*p[i-1]*n%P;
  for(ed=i=1;i<n;i++)scanf("%d%d%d",&x,&y,&z),add(x,y,z),add(y,x,z);
  f[0]=all=n;findroot(1,now=0);solve(now);
  sort(pool+1,pool+cp+1,cmp);
  for(i=1;i<=cp;i++)if(i==1||cmp(pool[i-1],pool[i]))pool[++_]=pool[i];
  cp=_;
  for(i=1;i<=cnt;i++)sort(q+st[i],q+en[i]+1,cmp);
  LA=LB=pool[1];
  RA=RB=ANSA=ANSB=pool[cp];
  /*for(i=1;i<=cp;i++)printf("%d ",sum[pool[i]]);puts("");
  for(i=1;i<=cnt;i++){
    printf("base=%d:",base[i]);
    for(j=st[i];j<=en[i];j++)printf(" %d",sum[q[j]]);
    puts("");
  }*/
  while(1){
    //(L,R)
    ct++;
    ll have=0;
    //printf("L=%d R=%d\n",sum[LA]+sum[LB],sum[RA]+sum[RB],sum[MA]+sum[MB]);
    for(i=1,j=cp+1,k=cp;i<=cp;i++){
      //(L,R)
      while(j-1>i&&compare(LA,LB,pool[i],pool[j-1])<0)j--;
      while(k>i&&compare(RA,RB,pool[i],pool[k])<=0)k--;
      //printf("->i=%d j=%d k=%d\n",i,j,k);
      j=max(j,i+1);
      pl[i]=j,pr[i]=k;
      if(j>i&&j<=k)have+=k-j+1;
    }
    if(have<=LIM){
      for(i=1;i<=cp;i++){
        j=pl[i],k=pr[i];
        //printf("->i=%d j=%d k=%d\n",i,j,k);
        if(j>i&&j<=k)for(x=j;x<=k;x++){
          e[++ce]=E(pool[i],pool[x]);
        }
      }
      break;
    }
    have=myrand()%have+1;
    for(i=1;i<=cp;i++){
      j=pl[i],k=pr[i];
      if(j>i&&j<=k){
        if(k-j+1>=have){
          MA=pool[i];
          MB=pool[pl[i]+have-1];
          break;
        }
        have-=k-j+1;
      }
    }
    //printf("L=%d R=%d M=%d\n",sum[LA]+sum[LB],sum[RA]+sum[RB],sum[MA]+sum[MB]);
    //evaluate mid
    total=0;
    for(i=1;i<=cnt;i++)if(base[i]==-1)dec(st[i],en[i]);
    for(i=1;i<=cnt;i++)if(base[i]==1)inc(st[i],en[i]);
    //printf("total=%lld\n",total);
    if(total>=K){
      ANSA=MA;
      ANSB=MB;
      RA=MA;
      RB=MB;
    }else{
      LA=MA;
      LB=MB;
    }
  }
  if(ce>1)sort(e+1,e+ce+1,cmpe);
  int l=1,r=ce;
  //for(i=1;i<=ce;i++)printf("%d ",sum[e[i].l]+sum[e[i].r]);puts("");
  while(l<=r){
    ct++;
    int mid=(l+r)>>1;
    MA=e[mid].l;
    MB=e[mid].r;
    total=0;
    for(i=1;i<=cnt;i++)if(base[i]==-1)dec(st[i],en[i]);
    for(i=1;i<=cnt;i++)if(base[i]==1)inc(st[i],en[i]);
    //printf("M=%d\n",sum[MA]+sum[MB]);
    //printf("total=%lld\n",total);
    if(total>=K){
      ANSA=MA;
      ANSB=MB;
      r=mid-1;
    }else{
      l=mid+1;
    }
  }
  //printf("ct=%d cp=%d tot=%d\n",ct,cp,tot);
  printf("%d",(sum[ANSA]+sum[ANSB])%P);
}