Codeforces Round 747 (Div. 2)
D
The Number of Imposters
Codeforces
1700
原题
← CE1 →
翻译中…

Theofanis started playing the new online game called "Among them". However, he always plays with Cypriot players, and they all have the same name: "Andreas" (the most common name in Cyprus).

翻译中…

In each game, Theofanis plays with $n$ other players. Since they all have the same name, they are numbered from $1$ to $n$.

翻译中…

The players write $m$ comments in the chat. A comment has the structure of "$i$ $j$ $c$" where $i$ and $j$ are two distinct integers and $c$ is a string ($1 \le i, j \le n$; $i \neq j$; $c$ is either imposter or crewmate). The comment means that player $i$ said that player $j$ has the role $c$.

翻译中…

An imposter always lies, and a crewmate always tells the truth.

翻译中…

Help Theofanis find the maximum possible number of imposters among all the other Cypriot players, or determine that the comments contradict each other (see the notes for further explanation).

翻译中…

Note that each player has exactly one role: either imposter or crewmate.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Description of each test case follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$; $0 \le m \le 5 \cdot 10^5$) — the number of players except Theofanis and the number of comments.

翻译中…

Each of the next $m$ lines contains a comment made by the players of the structure "$i$ $j$ $c$" where $i$ and $j$ are two distinct integers and $c$ is a string ($1 \le i, j \le n$; $i \neq j$; $c$ is either imposter or crewmate).

翻译中…

There can be multiple comments for the same pair of $(i, j)$.

翻译中…

It is guaranteed that the sum of all $n$ does not exceed $2 \cdot 10^5$ and the sum of all $m$ does not exceed $5 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, print one integer — the maximum possible number of imposters. If the comments contradict each other, print $-1$.

翻译中…

Note

翻译中…

In the first test case, imposters can be Andreas $2$ and $3$.

翻译中…

In the second test case, imposters can be Andreas $1$, $2$, $3$ and $5$.

翻译中…

In the third test case, comments contradict each other. This is because player $1$ says that player $2$ is an imposter, and player $2$ says that player $1$ is a crewmate. If player $1$ is a crewmate, then he must be telling the truth, so player $2$ must be an imposter. But if player $2$ is an imposter then he must be lying, so player $1$ can't be a crewmate. Contradiction.

翻译中…

样例

Input #1
5
3 2
1 2 imposter
2 3 crewmate
5 4
1 3 crewmate
2 5 crewmate
2 4 imposter
3 4 imposter
2 2
1 2 imposter
2 1 crewmate
3 5
1 2 imposter
1 2 imposter
3 2 crewmate
3 2 crewmate
1 3 imposter
5 0
Output #1
2
4
-1
2
5
InputCopy OutputCopy
题目信息
题号
1594D
时间限制
3 s
内存限制
256 MB
标签
构造
DFS 及类似
动态规划
并查集
图论

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