ICPC Teams
Time limit3sMemory limit256 MB
Count ways to split 3N students into teams of three so all M same-team and different-team pairs hold, modulo 1e9+9.
- Level
Hard8 of 10
- Topics
- Combinatorics, Union-find, Backtracking
- Solved
- No attempts yet
Problem
You coach the ICPC club at your university. The club has students, and you have to build teams for the next ICPC. Every ICPC team has exactly 3 members, and each student belongs to exactly one team.
When you build the teams you have to account for several relationships among the students. Two students who get along extremely well perform much better when they share a team. The opposite happens for a pair that gets along badly. So two students with a good relationship must be placed on the same team, and two students with a bad relationship must be placed on different teams. As the coach you know all relationships among the students.
Write a program that counts the team assignments satisfying every condition. Two assignments are different if and only if there is a pair of students who share a team in one assignment and do not share a team in the other.
Input
The first line contains two integers and (, ). The -th of the next lines contains three integers , (, ) and (). and are student numbers and is the relationship type. If is 0, student and student have a good relationship. If is 1, they have a bad relationship. For all with , holds.
Output
Print the number of valid team assignments modulo on one line.