time limit per test : 2 seconds
memory limit per test : 256 megabytes

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

You are also given an integer kkk . Consider sequences of kkk vertices. Let’s call a sequence [a1,a2,…,ak][a_1,a_2,…,a_k][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 a1a_1a1​ and ending at aka_kak​.
Start at a1a_1a1​, then go to a2a_2a2​ using the shortest path between a1a_1a1​ and a2a_2a2​, then go to a3a_3a3​ in a similar way, and so on, until you travel the shortest path between ak−1a_{k−1}ak−1​ and aka_kak​.
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=3k=3 then the following sequences are good: [1,4,7],[5,5,3][1,4,7], [5,5,3][1,4,7],[5,5,3] and [2,3,7][2,3,7][2,3,7]. The following sequences are not good: [1,4,6],[5,5,5],[3,7,3][1,4,6], [5,5,5], [3,7,3][1,4,6],[5,5,5],[3,7,3].

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

Input

The first line contains two integers nnn and k(2≤n≤105,2≤k≤100)k (2≤n≤10^5, 2≤k≤100)k(2≤n≤105,2≤k≤100), the size of the tree and the length of the vertex sequence.
Each of the next n−1n−1n−1 lines contains three integers uiu_iui​, viv_ivi​ and xi(1≤ui,vi≤n,xi∈0,1)x_i (1≤u_i,v_i≤n, x_i∈{0,1})xi​(1≤ui​,vi​≤n,xi​∈0,1), where ui and vi denote the endpoints of the corresponding edge and xi is the color of this edge (000 denotes red edge and 111 denotes black edge).
Output

Print the number of good sequences modulo 109+70^9+709+7.

Examples

4 4
1 2 1
2 3 1
3 4 1

Output

252

Input

4 6
1 2 0
1 3 0
1 4 0

Output

0

Input

3 5
1 2 1
2 3 0

Output

210

Note

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

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

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

题意:
给定一个n个节点的树,有n-1个染色的无向边(红色或者黑色),询问有多少个长度为k的序列a1,a2,a3...aka_1,a_2,a_3...a_ka1​,a2​,a3​...ak​(aia_iai​走到ai+1a_{i+1}ai+1​的路径是他们之间的最短路。)序列中元素可以重复,使得按顺序走过这个序列里面所有的点的路径中至少有一条黑色边。
题解:
并查集。可以知道如果不考虑其他东西的话一共有nkn^knk种路径,先把所有的红色边加入并查集,那么每个联通块内的元素所构成的序列一定是没有黑色边的。统计每个联通块内能组成的长度为kkk的不同的路径有多少条。然后用总条数减去即可。(记得要即时取模…不然会炸。)

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
#define MOD 1000000007LL
using namespace std;
int n,k;
struct edge{int u,v,w;
}e[200004];
int sz[100004];
ll ans,sum;
int f[100004];
int Find(int x){return f[x]==x?f[x]:f[x]=Find(f[x]);}
void Union(int x,int y){int p=Find(x),q=Find(y);if(p!=q){f[p]=q;sz[q]+=sz[p];sz[p]=0;}
}
ll  fp(ll x,ll y){if(y==0)return 1;ll temp=fp(x,y>>1);if(~y&1)return (temp*temp)%MOD;else return (((temp*temp)%MOD)*x)%MOD;
}
int w33ha(){ans=0;sum=0;for(int i=1;i<=n;i++){f[i]=i;sz[i]=1;}for(int i=1;i<n;i++){scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);if(e[i].w==0){Union(e[i].u,e[i].v);}}for(int i=1;i<=n;i++){if(sz[i]!=0){sum=(sum+fp(1LL*sz[i],k))%MOD;}}ans=1;for(int i=1;i<=k;i++){ans=(ans*n)%MOD;}ans=(ans-sum+MOD)%MOD;printf("%lld\n",ans);return 0;
}
int LiangJiaJun(){while(scanf("%d%d",&n,&k)!=EOF)w33ha();return 0;
}

[codeforces1139C]Edgy Trees相关推荐

  1. 【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 ...

  2. C. Edgy Trees(并查集+细节)

    C. Edgy Trees 思路: 总共有ans = POW(n,k)中可能,然后排出所有不可能的情况,每次序列中全部为红色或者只有一个点 的情况就不用考虑,所以可以用红色边建立连通图,然后枚举每一连 ...

  3. # 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 ...

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

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

  5. Edgy Trees

    https://codeforces.com/contest/1139/problem/C 题解:并查集+快速幂 /* *@Author: STZG *@Language: C++ */ #inclu ...

  6. C. Edgy Trees

    题目 ps;知道怎么做,代码不会写..... #include <iostream> #include <bits/stdc++.h> using namespace std; ...

  7. codeforces 1139c Edgy Trees 【并查集 】

    题意: 一颗有n个节点的树 树的边为红色或者黑色 给你节点数 n  和 k 让你求出长度为k的 1 - n 的全排列 作为路径在该树上  经过黑色的边 的数量  答案 mod 1e9+7 题解: 将思 ...

  8. Codeforces Round #548 C. Edgy Trees

    题面: 传送门 题目描述: 给出有n个节点的树,整数k.题目要求找长度为k,符合规则(good序列)的"点序列"(由节点构成的序列)个数有多少?规则如下: 1.走一条出发点为a1, ...

  9. Edgy Trees CodeForces - 1139C

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

最新文章

  1. 程序员都该懂点 HTTP
  2. Kubernetes — HOST-DEVICE CNI
  3. 让你轻松学会PHP版自动化SQL盲注工具-全库-全表-全字段-全字段值查询
  4. unity导出工程导入到iOS原生工程中详细步骤
  5. 菲涅尔效应(Fresnel Effect)
  6. mysql皮秒转成年月日时分秒_MySQL performance_schema 中 TIMER_*类字段 的易读转换
  7. 甜甜圈和拓扑学也有关系,你想的到吗?
  8. java打印结果横向排列_Java8排列组合(6行代码实现)
  9. python列表写入字典_python – 将列表字典写入CSV文件
  10. (68)信号发生器DDS协议(第14天)
  11. [C++] - C++11 多线程 - Mutex
  12. 形态学上的图像顶帽运算和黑帽运算是什么?
  13. sublime 插件(持续更新)
  14. 计算机组织原理答案白中英,计算机组成原理答案-白中英
  15. Java权限管理系统完整案例
  16. [100124]红楼梦:林黛玉与北静王【硬盘版】[带全CG存档+攻略]
  17. Unity3D C#数学系列之求点到直线的距离
  18. 如何理解IPD+CMMI+Scrum一体化研发管理解决方案之IPD?
  19. 【渝粤题库】陕西师范大学163107饭店管理 作业【高起专】
  20. 软件工程之结构化方法

热门文章

  1. hp服务器通过ilo5安装系统,HPE ProLiant Gen10 通过iLO 5(v1.15) web界面多种方式更新服务器固件,包含升级系统恢复集方法...
  2. 防止padding撑开盒子的方法
  3. 关于一些实用的资源网站,目前知道的
  4. 【以太坊】什么是雷电网络 Raiden network
  5. android开发中绕过检测,[原创] 某加固软件的一些检测点以及绕过方法,欢迎补充...
  6. 2021年中国饮料酒行业现状分析:产量有所下降[图]
  7. echarts图表x轴基准线(平行y轴)
  8. Python是如何将“中文”转“拼音”的?
  9. IAR代码溢出问题处理section placement failed
  10. 用python监控A股股票波动并发送预警邮件_V3