第一行输入三个整数
代表节点数、给定的上下限。
第二行输入
个整数
代表每个节点的权值。
此后
行,每行输入两个整数
代表一条无向边连接树上
和
两个节点。
在一行上输出一个整数,代表好路径的条数。
5 2 3 5 4 3 3 1 1 2 1 3 3 4 3 5
4
对于这个样例,如下图所示。路径
是好的,因为路径点权最小值
且点权最大值
。
除此之外,以下路径也是好的:
;
;
。
import sys
inps = sys.stdin.read().split('\n')
from collections import deque
def count(n, inSet, tree):
total = 0
visited = [False] * n
for i in range(n):
if not visited[i] and inSet[i]:
que = deque()
que.append(i)
_n = 1
visited[i] = True
while que:
u = que.popleft()
for v in tree[u]:
if not visited[v] and inSet[v]:
_n += 1
que.append(v)
visited[v] = True
total += _n * (_n - 1) // 2
return total
def main():
n, a, b = map(int, inps[0].split(' '))
ws = list(map(int, inps[1].split(' ')))
tree = [[] for _ in range(n)]
for uv in inps[2:-1]:
u, v = map(int, uv.split(' '))
tree[u - 1].append(v - 1)
tree[v - 1].append(u - 1)
total = n * (n - 1) // 2
inA = [w > a for w in ws]
inB = [w < b for w in ws]
inAB = [a < w < b for w in ws]
cntA = count(n, inA, tree)
cntB = count(n, inB, tree)
cntAB = count(n, inAB, tree)
oup = total - (cntA + cntB - cntAB)
print(oup)
if __name__ == '__main__':
main() line = [int(item) for item in input().split(" ")]
node_num = line[0]
min_num = line[1]
max_num = line[2]
node_val = [int(item) for item in input().split(" ")]
node_tu = [[0] * node_num for i in range(node_num)]
for i in range(0, node_num - 1):
line = [int(item) for item in input().split(" ")]
node_tu[line[0]-1][line[1]-1] = 1
node_tu[line[1]-1][line[0]-1] = 1
# print(node_tu)
resa = []
def get_next_path(node_tu, searched_path):
node = searched_path[-1]
res = []
for i in range(node_num):
if node_tu[node - 1][i] == 1 and i + 1 not in searched_path:
res.append(i + 1)
return res
def search_path(node_tu, searched_path, res):
next_path = get_next_path(node_tu, searched_path)
if not next_path:
val_list = []
for item in searched_path:
val_list.append(node_val[item - 1])
if min(val_list) <= min_num and max(val_list) >= max_num:
temp = list(reversed(searched_path))
if searched_path not in res and temp not in res:
res.append(searched_path)
else:
for node in next_path:
search_path(node_tu, searched_path + [node], res)
for node in range(1,node_num+1):
search_path(node_tu, [node,], resa)
print(len(resa))