-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1289.cpp
37 lines (34 loc) · 783 Bytes
/
1289.cpp
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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MAX = 100001;
const int MOD = 1000000007;
int n;
vector<pii> adj[MAX];
ll acc = 0;
ll solve(int prev, int cur) {
ll ret = 1;
for (auto i : adj[cur]) {
auto [next, cost] = i;
if (prev == next) continue;
ll tmp = (solve(cur, next) * cost) % MOD;
acc = (acc + ret * tmp) % MOD;
ret = (ret + tmp) % MOD;
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int answer = 0;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
solve(-1, 1);
cout << acc << "\n";
}