Breed Assignment
InterviewTime limit1sMemory limit128 MB
Count the breed assignments for N cows under same/different constraints, or report 0 if they conflict.
- Level
Easy3 of 10
- Topics
- Graph, Backtracking, Brute force, Implementation
- Solved
- No attempts yet
Problem
Farmer John has cows (), each of one of three breeds: Holstein, Jersey, or Guernsey.
Unfortunately, Farmer John cannot remember the exact breed of each cow. He does, however, remember a list of () relationships between pairs of cows. For example, he might remember that cows 1 and 2 have the same breed, or that cows 1 and 5 have different breeds.
Given Farmer John's list of relationships between pairs of cows, compute the number of different possible assignments of breeds to his cows. (This number can be if the list contains contradictory information.)
Input
- Line 1: Two space-separated integers and .
- Lines 2 through : Each line describes the relationship between two cows and (, ). It is either of the form
S x y, meaning and have the same breed, orD x y, meaning and have different breeds.
Output
- Line 1: The number of possible breed assignments.
Hint
For example, suppose there are cows where cows 1 and 2 have the same breed and cows 1 and 3 have different breeds. The first three cows admit six possible assignments: HHG, HHJ, GGH, GGJ, JJH, JJG. For each of them the fourth cow is unconstrained and may take any of the three breeds, giving assignments consistent with Farmer John's list.