题目链接

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);
}

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

  1. # Codeforces Round #548 (Div. 2)C Edgy Trees

    Codeforces Round #548 (Div. 2)C Edgy Trees 题目传送门 You are given a tree (a connected undirected graph ...

  2. Codeforces Round #548 (Div. 2)C. Edgy Trees 并查集

    codeforces 1139C 题目链接:http://codeforces.com/contest/1139/problem/C 题意: 给你一个n个结点n-1条边的无向连通图,由红边和黑边组成. ...

  3. C. Edgy Trees---(思维题+并查集的运用)---Codeforces Round #548 (Div. 2)

    Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes 题目链接http://codeforces.c ...

  4. [题解][Codeforces 1139A~1139F]Codeforces Round #548 (Div. 2) 简要题解

    终于 rank < 10 了 题目 洛谷 RemoteJudge A B C D E F Codeforces A B C D E F A 题意 一个长度为 nnn 的数字串 sss 求 sss ...

  5. Codeforces Round #548 (Div. 2) C. Edgy Trees(dfs || 并查集)

    题目链接:https://codeforces.com/contest/1139/problem/C 题意:给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路 ...

  6. Codeforces Round #548 (Div. 2) C. Edgy Trees(思维+dfs)

    题目链接:https://codeforces.com/contest/1139/problem/C        题意是给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak ...

  7. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  8. Codeforces Round #548 (Div. 2), problem: (C) Edgy Trees 【并查集+快速幂】

    题意 给了一棵树,n个点,m条边.让从中选k个点,使得从a1到a2,a2到a3,ak-1到ak的路径中至少经过一条黑色的边,问这样的集合有多少个 思路 用并查集统计一个连通块的节点个数,最后用总的减去 ...

  9. Codeforces Round #548 (Div. 2) C. Edgy Trees(并查集+快速幂)

    思路用并查集统计一个连通块的节点个数,最后用总的减去他,设x是连通块的节点个数,o个联通块 #include<bits/stdc++.h> #define fi first #define ...

最新文章

  1. Data - 深入浅出学统计 - 下篇
  2. httpd四之CGI、HTTPS、压缩配置
  3. python设置函数_在Python中设置函数签名
  4. bestcoder #66
  5. 更好也更快!最先进的图像去模糊算法DeblurGAN-v2
  6. csv文件转换成html,jQuery 把CSV文件数据转换为HTML表格(Bootstrap Table)
  7. 技术圈儿002---高并发整体可用性:一文详解降级、限流和熔断
  8. Java 获取系统信息
  9. 6. ubuntu 下 mysql 数据库迁移
  10. 101名女职工血清总胆固醇测量结果spss描述统计分析
  11. [转] A trip through the Graphics Pipeline
  12. Windows上部署Discuz论坛
  13. PYMOL-note
  14. 图片批量旋转与翻转工具
  15. 计算广告概述【计算广告】
  16. c语言递推算法微课,高中数学题型方法100讲[微课视频]
  17. 我不接私活了,抱歉!
  18. 我每天都要打开的8个在线网站,很有用~
  19. Lua入门(1) 编译环境、变量类型与三种结构的实现
  20. Python中有“不相等”的运算符吗?

热门文章

  1. 怎么用计算机给u盘加密文件,电脑如何使用bitlocker功能给u盘加密
  2. 小甲鱼31课泡菜课后作业反思
  3. 蓝桥杯 青少年创意编程大赛 scratch组 (三)
  4. CSS------第四章浮动
  5. 团队作业2——团队计划
  6. 响铃:抖音的敌人不是快手
  7. win10计算机休眠后无法唤醒,win10电脑休眠后无法唤醒的解决办法
  8. 基于协同过滤算法的电影推荐系统设计(二) - ALS算法详解
  9. android 备份管理器,最佳Android备份提取器和备份解决方案
  10. 戴尔笔记本重装系统后识别不出固态盘的问题的方法