-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC1139.cpp
More file actions
76 lines (60 loc) · 1005 Bytes
/
Copy pathC1139.cpp
File metadata and controls
76 lines (60 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
vector<int> adj[200001];
bool visited[200001] = {0};
int power(int x, int y)
{
int res = 1;
//x = x%MOD;
while (y > 0)
{
if (y & 1)
res = (res*x) % MOD;
y = y>>1;
x = (x*x)%MOD;
}
return res;
}
int sz;
void dfs(int s)
{
if(visited[s]) return;
sz++;
visited[s] = true;
for(int u : adj[s])
dfs(u);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, k; cin >> n >> k;
int ans = power(n, k);
for(int i = 0; i < n-1; i++)
{
int u, v, x;
cin >> u >> v >> x;
if(x == 0)
{
adj[u].pb(v);
adj[v].pb(u);
}
}
int sum = 0;
for(int i = 1; i < n+1; i++)
{
if(visited[i]) continue;
sz = 0;
dfs(i);
ans -= power(sz, k);
ans += MOD;
ans %= MOD;
}
cout << ans << '\n';
return 0;
}