Repository navigation
Expand file tree
/
Copy path1139C.cpp
More file actions
88 lines (67 loc) · 1.65 KB
/
Copy path1139C.cpp
File metadata and controls
88 lines (67 loc) · 1.65 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
/*
بِسْمِ اللهِ الرَّحْمٰنِ الرَّحِيْمِ
In the name of Allah, the Most Gracious, the Most Merciful.
*/
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int MOD = 1e9 + 7 ;
long long pow( long long u , long long v){
long long ans = 1 ;
while( v ){
if(v & 1){
ans = (ans * u )%MOD ;
}
u = (u*u)%MOD;
v >>= 1;
}
return ans ;
}
int dfs( int u , vector<vector<int>> &gr , vector<bool > &vis ){
int sum = 0 ;
vis[u] = true;
for( auto v : gr[u]){
if(!vis[v]){
sum += dfs( v, gr , vis ) ;
}
}
return sum + 1 ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
//cin >> t;
while(t--){
int n;
cin >> n;
int k;
cin >> k ;
// total - bad ez
// size of each connected comp -> sigma -> subtract
vector< vector<int>> gr( n) ;
for (int i = 0; i < n-1 ; i++) {
int u , v ;
cin >> u >> v;
int col ;
cin >> col ;
u--;
v--;
if(!col){
gr[u].push_back(v);
gr[v].push_back(u);
}
}
vector< bool > vis( n , false );
long long ans = 0 ;
for (int i = 0; i < n; i++) {
if( vis[i])
continue;
int s = dfs( i , gr , vis ) ;
ans = (ans + pow( s , k))%MOD ;
}
cout << (pow( 1ll*n , 1ll*k ) - ans + MOD )%MOD << '\n';
}
return 0;
}