Under construction

Still being built. Things may change

Breed Assignment

Interview

Time limit1sMemory limit128 MB

Summary
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 NN cows (2≤N≤152 \le N \le 15), 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 KK (1≤K≤501 \le K \le 50) 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 00 if the list contains contradictory information.)

Input

  • Line 1: Two space-separated integers NN and KK.
  • Lines 2 through K+1K+1: Each line describes the relationship between two cows xx and yy (1≤x,y≤N1 \le x, y \le N, x≠yx \ne y). It is either of the form S x y, meaning xx and yy have the same breed, or D x y, meaning xx and yy have different breeds.

Output

  • Line 1: The number of possible breed assignments.

Hint

For example, suppose there are 44 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 6×3=186 \times 3 = 18 assignments consistent with Farmer John's list.

Examples1

  1. Example 1

    Input
    4 2
    S 1 2
    D 1 3
    
    Expected output
    18