Codeforces Round 309 (Div. 1)
C
Love Triangles
Codeforces
2200
原题
← BD →
翻译中…

There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has $*n*$ characters. The characters are labeled from $1$ to $*n*$. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state).

翻译中…

You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A).

翻译中…

You are given a list of $*m*$ known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo $1 000 000 007$.

翻译中…

Input

翻译中…

The first line of input will contain two integers $*n*, *m*$ ($3 ≤ *n* ≤ 100 000$, $0 ≤ *m* ≤ 100 000$).

翻译中…

The next $*m*$ lines will contain the description of the known relationships. The $*i*$-th line will contain three integers $*a*_{*i*}, *b*_{*i*}, *c*_{*i*}$. If $*c*_{*i*}$ is 1, then $*a*_{*i*}$ and $*b*_{*i*}$ are in love, otherwise, they hate each other ($1 ≤ *a*_{*i*}, *b*_{*i*} ≤ *n*$, $*a*_{*i*} ≠ *b*_{*i*}$, image).

翻译中…

Each pair of people will be described no more than once.

翻译中…

Output

翻译中…

Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo $1 000 000 007$.

翻译中…

Note

翻译中…

In the first sample, the four ways are to:

翻译中…
  • Make everyone love each other
  • Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this).
翻译中…

In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.

翻译中…

样例

Input #1
3 0
Output #1
4
InputCopy OutputCopy
Input #2
4 4
1 2 1
2 3 1
3 4 0
4 1 0
Output #2
1
InputCopy OutputCopy
Input #3
4 4
1 2 1
2 3 1
3 4 0
4 1 1
Output #3
0
InputCopy OutputCopy
题目信息
题号
553C
时间限制
2 s
内存限制
256 MB
标签
DFS 及类似
并查集
图论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。