【Codeforces Round #548(Div. 2)】Edgy Trees(数学+bfs求连通块)

题目链接

C. Edgy Trees

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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-1组边,求在这组边中能找到多少个由k个顶点的组成的序列,在这个序列中一定存在一条黑边。

【解题思路】

思路要转换一下,即求所有的组合 - 有且仅与黑边相连的顶点的个数 - 红边的连通块。每个红边的连通块只需计算连通块内顶点的个数num,那么方案数就等于num^k。

【代码】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const LL mod=1e9+7;
int f[maxn],f2[maxn],vis[maxn];
LL sum=0,n,k;
vector<LL>v[maxn];
map<int,int>mp;
LL quickpow(LL a,LL b)
{
    LL ans=1;
    while(b!=0)
    {
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans%mod;
}
void bfs(int x)
{
    queue<int>q;
    q.push(x);
    int num=1;
    vis[x]=1;
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        for(int i=0;i<v[t].size();i++)
        {
            int p=v[t][i];
            if(!vis[p])
            {
                //printf("p=%d\n",p);
                vis[p]=1;
                q.push(p);
                num++;
            }
        }
    }
    sum=(sum+quickpow(num,k))%mod;
}
int main()
{
    int cnt=0;
    scanf("%I64d%I64d",&n,&k);
    for(int i=0;i<n-1;i++)
    {
        int uu,vv,w;
        scanf("%d%d%d",&uu,&vv,&w);
        if(w==1)f[uu]=1,f[vv]=1;
        if(w==0)
        {
            f2[uu]=1;
            f2[vv]=1;
            v[uu].push_back(vv);
            v[vv].push_back(uu);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(f[i] && !f2[i])cnt++;
    }
    for(int i=1;i<=n;i++)
    {
        if(f2[i] && !vis[i])
        {
            bfs(i);
            //printf("%lld\n",sum);
        }
    }
    //printf("%lld\n",quickpow(n,k));
    LL ans=(quickpow(n,k)-cnt-sum+mod)%mod;
    printf("%I64d\n",ans);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值