Approach
Depth-first search
For ABC310 D — Peaceful Teams, 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
- 62 lines of Python from the credited upstream file abc310_d.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 4def main():5 import sys6 7 sys.setrecursionlimit(10**8)8 9 input = sys.stdin.readline10 11 n, t, m = map(int, input().split())12 13 14 15 ng = [[] for _ in range(n)]16 17 for _ in range(m):18 ai, bi = map(int, input().split())19 ai -= 120 bi -= 121 22 ng[ai].append(bi)23 ng[bi].append(ai)24 25 def dfs(now, teams):26 if now == n:27 if len(teams) == t:28 return 129 else:30 return 031 32 count = 033 34 35 for team in teams:36 ok = True37 38 for player in team:39 if player in ng[now]:40 ok = False41 break42 43 if ok:44 team.append(now)45 count += dfs(now + 1, teams)46 team.pop()47 48 49 if len(teams) < t:50 teams.append([now])51 count += dfs(now + 1, teams)52 teams.pop()53 54 return count55 56 ans = dfs(0, [])57 print(ans)58 59 60if __name__ == "__main__":61 main()62