C. Edgy Trees(并查集+数学)

35 篇文章 0 订阅
21 篇文章 0 订阅

You are given a tree (a connected undirected graph without cycles) of nn vertices. Each of the n−1n−1 edges of the tree is colored in either black or red.

You are also given an integer kk. Consider sequences of kk vertices. Let's call a sequence [a1,a2,…,ak][a1,a2,…,ak] good if it satisfies the following criterion:

  • We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from a1a1 and ending at akak.
  • Start at a1a1, then go to a2a2 using the shortest path between a1a1 and a2a2, then go to a3a3 in a similar way, and so on, until you travel the shortest path between ak−1ak−1 and akak.
  • If you walked over at least one black edge during this process, then the sequence is good.

Consider the tree on the picture. If k=3k=3 then the following sequences are good: [1,4,7][1,4,7], [5,5,3][5,5,3] and [2,3,7][2,3,7]. The following sequences are not good: [1,4,6][1,4,6], [5,5,5][5,5,5], [3,7,3][3,7,3].

There are nknk sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo 109+7109+7.

Input

The first line contains two integers nn and kk (2≤n≤1052≤n≤105, 2≤k≤1002≤k≤100), the size of the tree and the length of the vertex sequence.

Each of the next n−1n−1 lines contains three integers uiui, vivi and xixi (1≤ui,vi≤n1≤ui,vi≤n, xi∈{0,1}xi∈{0,1}), where uiui and vivi denote the endpoints of the corresponding edge and xixi is the color of this edge (00 denotes red edge and 11 denotes black edge).

Output

Print the number of good sequences modulo 109+7109+7.

Examples

input

Copy

4 4
1 2 1
2 3 1
3 4 1

output

Copy

252

input

Copy

4 6
1 2 0
1 3 0
1 4 0

output

Copy

0

input

Copy

3 5
1 2 1
2 3 0

output

Copy

210

Note

In the first example, all sequences (4444) of length 44 except the following are good:

  • [1,1,1,1][1,1,1,1]
  • [2,2,2,2][2,2,2,2]
  • [3,3,3,3][3,3,3,3]
  • [4,4,4,4][4,4,4,4]

In the second example, all edges are red, hence there aren't any good sequences.

题解:计算在n个点中任意选取k个点,其各点之间最短路径只要经过1条黑色边即为GOOD,那么反向思维:找出一类集合,其各点之间只有红色连边,其中每个集合的个数的k次方即为在这个集合的路径,是NOT GOOD的,并查集建立集合,循环遍历一遍求出所有NOT GOOD的排列个数,用总数一减即可。

感想:其实很简单的一道题,打比赛时也想到了这一点,但思维不清晰,导致想不到怎么去实现它~~

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=100010;
const int mod=1000000007;
int n,k,u,v,w,ans,f[maxn],x[maxn];
void Init()
{
    for(int i=1;i<=n;i++)
        f[i]=i;
}
int getf(int x)
{
    return f[x]==x?x:f[x]=getf(f[x]);
}
void Merge(int u,int v)
{
    u=getf(u);
    v=getf(v);
    if(u!=v)
        f[u]=v;
}
ll quickpow(ll a,ll b)//快速幂运算,由于数据比较小,直接暴力b次幂也能过
{
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    cin>>n>>k;
    Init();
    for(int i=1;i<n;i++)
    {
        cin>>u>>v>>w;
        if(w==0)
            Merge(u,v);//将所有红色连边的点合并
    }
    for(int i=1;i<=n;i++)//计算每一个红色集合中的个数
        x[getf(i)]++;
    for(int i=1;i<=n;i++)
    {
        if(f[i]==i)//为一个集合
            ans=(ans+quickpow(x[i],k))%mod;//在x[i]个元素中任意选取k个
    }
    cout<<(quickpow(n,k)-ans+mod)%mod<<endl;//总和减去ans即为答案了~~
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值