luogu#P4815. [CCO 2014] 狼人游戏

[CCO 2014] 狼人游戏

Problem Description

This problem is translated from CCO 2014 Day 1 T3「Werewolf」.

As usual, NN robots are playing a game of werewolf. The robots are numbered from 11 to NN. Exactly WW robots are werewolves, and the rest are villagers. Although the game of werewolf can be studied from different angles, we will focus on just one aspect.

Robots may accuse others of being werewolves, and they may also protect other robots from being falsely accused.

Werewolves know everyone’s roles, and:

  • A werewolf never accuses another werewolf.
  • Any robot protected by a werewolf is also a werewolf.

Villagers may accuse or protect robots of any type.

Some additional restrictions make the problem easier:

  • No robot is both accused and protected.
  • No robot is accused or protected more than once.
  • If a robot with number AA accuses or protects a robot with number BB, then we guarantee that A<BA < B.

You are given all accusation and protection relationships among the NN robots, as well as the number of werewolves WW. Each robot’s role is either werewolf or villager. Your task is to count the number of role assignments that satisfy all the above restrictions.

Input Format

The first line contains three integers N,W,MN, W, M, representing the number of robots, the number of werewolves, and the number of accusation/protection relationships.

The next MM lines each describe one relationship. Each line is in one of the following two forms:

  • A a b, meaning robot aa accuses robot bb of being a werewolf.
  • D a b, meaning robot aa protects robot bb.

Output Format

Output the number of role assignments that satisfy the conditions above. Since the result can be very large, output it modulo 109+710^9 + 7.

2 1 1
D 1 2
1
2 1 0
2
3 2 2
A 1 2
D 1 3
2

Hint

Sample Explanation 1

If robot 11 is a werewolf, then robot 22 must also be a werewolf, which would give too many werewolves. The only possibility is that robot 22 is the only werewolf.

Sample Output 2

If there is no additional protection or accusation information, then both robot 11 and robot 22 could be the werewolf.

Sample Explanation 3

If robot 11 is a werewolf, then robot 22 will be a villager and robot 33 will also be a werewolf; or if robot 11 is a villager, then robots 22 and 33 will be werewolves.

For 20%20\% of the testdata, 1N201 \le N \le 20.

For 100%100\% of the testdata, 1N2001 \le N \le 200, 0WN0 \le W \le N, 0M<N0 \le M < N.

Translated by ChatGPT 5