Last active
May 8, 2019 18:06
-
-
Save Rockbet/01501e957eaff572bad37bd9c12e8daa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int MAXN = 1e5 + 100; | |
const long long mod = 1e9+7; | |
vector<int> grafo[MAXN]; | |
bool mark[MAXN]; | |
long long p; | |
int dfs(int u){ | |
mark[u]=1; | |
for(int i=0; i<grafo[u].size(); i++){ | |
int v = grafo[u][i]; | |
if(!mark[v]){ | |
p++; | |
dfs(v); | |
} | |
} | |
} | |
long long fast_exponentiation(long long b, long long exp){ | |
long long ans = 1; | |
for(; exp > 0; exp /= 2){ | |
if(exp & 1) ans = ans * b % mod; | |
b = b * b % mod; | |
} | |
return ans; | |
} | |
int main(){ | |
ios_base::sync_with_stdio(false);cin.tie(NULL); | |
long long n, k; | |
cin >> n >> k; | |
for(int i=1; i<n; i++){ | |
int x, y, z; | |
cin >> x >> y >> z; | |
if(z==0){ | |
grafo[x].push_back(y); | |
grafo[y].push_back(x); | |
} | |
} | |
long long ans = fast_exponentiation(n, k); | |
for(int i=1; i<=n; i++){ | |
if(!mark[i]){ | |
p=1; | |
dfs(i); | |
ans = (mod+ans-fast_exponentiation(p, k))%mod; | |
} | |
} | |
cout << (mod+ans)%mod << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment