-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsolve.py
58 lines (47 loc) · 870 Bytes
/
solve.py
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
import sys
mod = 10 ** 9 + 7
def read_and_split():
ret = []
for i in input().split():
ret.append(int(i))
return ret
n = read_and_split()[0]
graf = []
for i in range(n):
graf.append([])
for i in range(1, n):
a, b = read_and_split()
a -= 1
b -= 1
graf[a].append(b)
graf[b].append(a)
def choose(d, at_least):
global mod
ret = 0
x = 1
if at_least == 0:
ret = 1
for i in range(1, d + 1):
x = (x * (d + 1 - i)) % mod
if i >= at_least:
ret += x
return ret % mod
wyn = n - 1
for i in range(n):
wyn += choose(len(graf[i]), 2)
wyn %= mod
def dfs(v, oj):
global graf, wyn, mod
d = len(graf[v])
ret = choose(d - 1, 1)
mul = choose(d - 2, 0)
for i in graf[v]:
if i == oj:
continue
take = dfs(i, v)
wyn = (wyn + 2 * ret * take) % mod
ret = (ret + mul * take) % mod
return ret
sys.setrecursionlimit(10 ** 6)
dfs(0, -1)
print(wyn)