Love Triangles
Time limit2sMemory limit512 MB
Count the ways to label every remaining pair in a graph as like or dislike so that no triple has exactly two liking pairs, given some fixed edges.
- Level
Hard8 of 10
- Topics
- Graph, Combinatorics, Union-find, Math
- Solved
- No attempts yet
Problem
A love triangle appears in many dramas and films. Character A and character B like each other, character B and character C like each other, but character A and character C dislike each other. For convenience, assume that every pair of characters either likes each other or dislikes each other. No pair is neutral, no pair is left undecided, and no pair is one sided with A liking B while B dislikes A. So for each of the pairs, like or dislike is fixed.
Consider a drama with characters. Alice knows the relation of pairs from the story so far. Alice is tired of stale love triangles and wants none of them in this drama. Alice also does not want all the characters to dislike one another, so among any three of them at least one pair must like each other. That is, for any three characters A, B, C, either exactly one of the pairs A-B, B-C, C-A likes each other, or all three pairs like each other.
Alice started drawing possible relation charts by filling in the pairs she does not know yet, but so many charts satisfy the condition that she gave up on drawing them all. She wants to count them instead. Given the relations already known, print the number of ways to fill in the remaining pairs.
Input
The first line contains the number of characters and the number of known relations . (, , )
Each of the next lines contains one relation. Line contains three positive integers , , . If is 1, then and like each other. If is 0, then they dislike each other. (, is 0 or 1)
The same pair is given at most once. If is given, then does not appear in the input.
Output
Print the number of ways to fill in the remaining pairs, modulo (), on one line.