The Great Revegetation (Silver)

Interview

Time limit2sMemory limit512 MB

Summary
Count the binary assignments of grass types to N pastures that satisfy M same-or-different constraints on pairs, and print the count in binary.
Level

Medium6 of 10

Topics
Union-find, Graph, DFS, Combinatorics
Solved
No attempts yet

Problem

A lengthy drought has left Farmer John's NN pastures devoid of grass. However, with the rainy season arriving soon, the time has come to "revegetate". In Farmer John's shed, he has two buckets, each with a different type of grass seed. He wants to plant grass in each of his NN pastures, choosing exactly one type of grass to plant in each.

Being a dairy farmer, Farmer John wants to make sure he manages the somewhat particular dietary needs of his MM cows. Each of his MM cows has two favorite pastures. Some of his cows have a dietary restriction that they should only eat one type of grass consistently, so Farmer John wants to make sure the same type of grass is planted in the two favorite fields of any such cow. Other cows have a very different dietary restriction, requiring them to eat different types of grass. For those cows, Farmer John of course wants to make sure their two favorite fields contain different grass types.

Please help Farmer John determine the number of different ways he can plant grass in his NN pastures.

Input

The first line of input contains NN (2≤N≤1052 \leq N \leq 10^5) and MM (1≤M≤1051 \leq M \leq 10^5). Each of the next MM lines contains a character that is either 'S' or 'D', followed by two integers in the range 1…N1 \ldots N, describing the pair of pastures that are the two favorites for one of Farmer John's cows. If the character is 'S', this line represents a cow that needs the same type of grass in its two favorite pastures. If the character is 'D', the line represents a cow that needs different grass types.

Output

Output the number of ways Farmer John can plant grass in his NN pastures. Please write your answer in binary.

Examples1

  1. Example 1

    Input
    3 2
    S 1 2
    D 3 2
    
    Expected output
    10