- T1(508C)
Examples
input
4 1
1 1 0 0
1 2
1 3
1 4
output
2
input
7 1
1 0 1 1 0 0 0
1 2
1 3
2 4
2 5
3 6
3 7
output
2
第一行n, k为节点个数,和最大容忍值
第二行n个数,为第i个节点有没有猫
后面n-1行,x,y 表示节点x和y有连接
从节点1出发,问到达叶子节点,且路上连续遇到猫个数步超过k
1.构建树结构
2.从1出发,若连续遇到多余k只猫,此路不同
3.到达叶子节点且连续遇到猫步超过k,ans++
4.dfs遍历每条路
代码(GO):
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
type tree_node struct {
children [](*tree_node)
value int
}
func visit(root, parent *tree_node, m, cur int, ans *int) {
if cur > m {
return
}
if len(root.children) == 1 && root.children[0] == parent {
*ans++
return
}
for i := 0; i < len(root.children); i++ {
if root.children[i] != parent {
if root.children[i].value == 0 {
visit(root.children[i], root, m, 0, ans)
} else {
visit(root.children[i], root, m, 1+cur, ans)
}
}
}
}
func main() {
var n, m int
fmt.Scan(&n, &m)
nodes := make([]tree_node, n+1, n+1)
bs := bufio.NewScanner(os.Stdin)
bs.Split(bufio.ScanWords)
for i := 1; i <= n; i++ {
bs.Scan()
nodes[i].value, _ = strconv.Atoi(bs.Text())
}
for i := 1; i < n; i++ {
bs.Scan()
p, _ := strconv.Atoi(bs.Text())
bs.Scan()
c, _ := strconv.Atoi(bs.Text())
nodes[p].children = append(nodes[p].children, &nodes[c])
nodes[c].children = append(nodes[c].children, &nodes[p])
}
ans := 0
visit(&nodes[1], nil, m, nodes[1].value, &ans)
fmt.Println(ans)
}
T2
T3
T4
T5
T6
T7
T8
T9
T10