欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1139C.Edgy Trees
Edgy Trees
You are given a tree (a connected undirected graph without cycles) of vertices. Each of the edges of the tree is colored in either black or red.
You are also given an integer . Consider sequences of vertices. Let's call a sequence good if it satisfies the following criterion:
- We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from and ending at .
- Start at , then go to using the shortest path between and , then go to in a similar way, and so on, until you travel the shortest path between and .
- If you walked over at least one black edge during this process, then the sequence is good.

Consider the tree on the picture. If then the following sequences are good: , and . The following sequences are not good: , , .
There are sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo .
Input
The first line contains two integers and (, ), the size of the tree and the length of the vertex sequence.
Each of the next lines contains three integers , and (, ), where and denote the endpoints of the corresponding edge and is the color of this edge ( denotes red edge and denotes black edge).
Output
Print the number of good sequences modulo .
Note
In the first example, all sequences () of length except the following are good:
In the second example, all edges are red, hence there aren't any good sequences.
Samples
4 4
1 2 1
2 3 1
3 4 1
252
4 6
1 2 0
1 3 0
1 4 0
0
3 5
1 2 1
2 3 0
210
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |