当前位置: 代码迷 >> 综合 >> Codeforces Round #548 (Div. 2) C. Edgy Trees
  详细解决方案

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

热度:92   发布时间:2023-12-12 17:32:43.0

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.

 

题意就是给你一颗树,树上有0和1的边(无向边),问你存在多少个序列经过一条1边,并且路过k个点,可以原地滞留,也可以折返

比如样例1,全是黑边就是4^4 - 4 (就是减去4种原点滞留的)

所以很容易可以想到用全部的 - 所有0边连通的

所以快速幂一下。用并查集维护联通快,可以得知经过k个点,有n条边的所有种数是n^k,最后注意相减的时候+mod,不然会减爆(因为自己wa了一发,切记 切记)

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se secondusing namespace std;typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 1e6;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp1(db a,db b){ return sign(a - b);}
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
void ex_gcd(ll a, ll b, ll &x, ll &y){if (b == 0) { x = 1;y = 0;return; } else {  ex_gcd(b, a%b, x, y);    ll t = x;       x = y;       y = t - (a / b)*y;   } return ; }
int fa[maxn];
int Find(int x) { if(x != fa[x]) return fa[x] = Find(fa[x]);  return fa[x];}
ll c,n,k;
ll  vis[maxn];
int main() {string str;ios::sync_with_stdio(false);while(cin >> n >> k){for(int i = 1; i<= n;i++) fa[i] = i;for(int i = 1;i < n;i++){int u,v,col;cin >> u >> v >> col;if(col == 0){int X = Find(u);int Y = Find(v);if(X != Y){fa[X] = Y;}}}ll ans = mul(n,k,mod) % mod;set<int>s;for(int i = 1;i <= n;i++){fa[i] = Find(i);s.insert(fa[i]);}for(int i = 1;i <= n;i++){vis[fa[i]]++;}ll cur = 0;for(auto d:s){cur += mul(vis[d],k,mod);cur %= mod;}cout << (ans + mod - cur) % mod << endl;}cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;return 0;
}

 

  相关解决方案