-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
68 lines (58 loc) · 1.31 KB
/
solution.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
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
#include<cstdio>
#include<vector>
using namespace std;
const int MOD = 1000000007;
const int MAXN = 100000;
vector<int> v[MAXN + 1];
bool visited[MAXN + 1];
int connected[MAXN + 1];
void dfs(int now, int num)
{
connected[num]++;
visited[now] = true;
int sz = (int)v[now].size();
for (int i = 0; i < sz; i++)
if (!visited[v[now][i]])
dfs(v[now][i], num);
}
int dp[MAXN + 1][4];
// driver program
int main()
{
// number of verticies of tree
int n;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++)
{
// input edges and colors
int a, b;
char c;
scanf("%d %d %c", &a, &b, &c);
if (c == 'b')
{
v[a].push_back(b);
v[b].push_back(a);
}
}
int conn = 0;
for (int i = 1; i <= n; i++)
{
if (!visited[i])
{
conn++;
dfs(i, conn);
}
}
for (int i = 1; i <= conn; i++)
dp[i][1] = dp[i - 1][1] + connected[i];
// If the answer is greater than 10^9 + 7, print the answer modulo (%) 10^9 + 7
for (int i = 2; i <= 3; i++)
{
for (int j = i; j <= conn; j++)
{
dp[j][i] = (dp[j - 1][i] + (long long)dp[j - 1][i - 1] * connected[j]) % MOD;
}
}
printf("%d\n", dp[conn][3]);
return 0;
}