1139C C. Edgy Trees

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

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

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

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

Each of the next n−1 lines contains three integers ui, vi and 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 (0 denotes red edge and 1 denotes black edge).

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

input
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 (44) of length 4 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条边的图,用1表示两点间的线是黑色的,用0表示两点间的线是红色的,求经过k个点和至少一条黑色边的点的集合的个数。
思路: 不考虑至少经过一条黑色边,共n^k个数,再减去只经过红色边的点的集合和黑色连接的单独点的数目,即为答案,其中只经过红色边的点的集合用领接表存储各点之间的连接,再统计出每个连通块点的数目num,则该连通块的点集合共num ^ k个,将其全部相加取模,详情看代码。

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
const ll mod = 1e9 + 7;
int n, k;
vector<vector<int> > v(N + 1);
bool vis[N];  
ll ksm(ll x, ll n, ll mod) { // 快速幂
	ll res = 1;
	while(n > 0) {
		if(n & 1) res = res * x % mod;
		x = x * x % mod;
		n >>= 1;
	}
	return res;
}
int main() {
	int a, b, c, num;
	ll sum = 0, ans, kk = 0;
	scanf("%d %d", &n, &k);
	for(int i = 0; i < n-1; i++) {
		scanf("%d %d %d", &a, &b, &c);
		if(c == 0) { // 记录红线连接的点的情况 
			v[a].push_back(b);
			v[b].push_back(a);
		}
	}
	for(int i = 1; i <= n; i++) {
		if(v[i].size() != 0) { // 判断该点是否是红线连接的 
			num = 1; // 对该连通块进行计数 
			queue<int> q;
			if(!vis[i]) { // 如果该点未被记录过,入队 
				q.push(i); 
				vis[i] = true; // 标记该点 
			}
			while(!q.empty()) { 
				int top = q.front(); // 取队首元素 
				q.pop(); // 出队 
				for(int j = 0; j < v[top].size(); j++) {
					if(!vis[v[top][j]]) { // 如果该点未被记录过,入队 
						q.push(v[top][j]);
						vis[v[top][j]] = true;  // 标记该点 
						num++; // 点的个数加一 
					}
				}
			}
			if(num != 1) {
				sum += ksm(num, k, mod); // 该连通块点的集合个数 
				kk += num; // 统计用红线相连的点的数目,n-kk就为单独黑色线连接点的数目 
				sum %= mod; 
			} 
		}
	}
	ans = ksm(n, k, mod) - sum - (n - kk);
	if(ans < 0) ans = mod + ans;
	printf("%I64d\n", ans);
	return 0;
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值