Approach
Depth-first search
For ABC133 E — Virus Tree 2, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- Combine child results or undo the choice when the branch finishes.
Code notes
- 52 lines of Python from the credited upstream file abc133_e.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected, together with recursive traversal.
Complexity
Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4from sys import setrecursionlimit5 6 7def main():8 import sys9 10 input = sys.stdin.readline11 12 setrecursionlimit(10 ** 7)13 14 n, k = map(int, input().split())15 graph = [[] for _ in range(n)]16 17 for _ in range(n - 1):18 ai, bi = map(int, input().split())19 ai -= 120 bi -= 121 22 graph[ai].append(bi)23 graph[bi].append(ai)24 25 mod = 10 ** 9 + 726 27 def dfs(current, rest_color, parent=-1):28 count = rest_color29 30 used_color_count = 131 32 if parent != -1:33 used_color_count += 134 35 for to in graph[current]:36 if to == parent:37 continue38 39 count *= dfs(to, k - used_color_count, current)40 count %= mod41 used_color_count += 142 43 return count44 45 ans = dfs(0, k)46 47 print(ans)48 49 50if __name__ == "__main__":51 main()52