CF1594D.The Number of Imposters

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn other players. Since they all have the same name, they are numbered from 11 to nn.

The players write mm comments in the chat. A comment has the structure of "ii jj cc" where ii and jj are two distinct integers and cc is a string (1≤i,j≤n1 \le i, j \le n; i≠ji \neq j; cc is either imposter or crewmate). The comment means that player ii said that player jj has the role cc.

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.

西奥法尼斯开始玩一款名为“Among Us”的新在线游戏。然而,他总是和塞浦路斯玩家一起玩,而这些玩家的名字全都一样:“安德烈亚斯”(塞浦路斯最常见的名字)。

在每局游戏中,西奥法尼斯与 nn 名其他玩家一同游戏。由于他们名字完全相同,因此被编号为 11 到 nn。

玩家们在聊天中总共发表了 mm 条评论。每条评论的格式为 “ii jj cc”,其中 ii 和 jj 是两个互异的整数,cc 是一个字符串(满足 1≤i,j≤n1 \le i, j \le n;i≠ji \neq j;cc 取值为 imposter 或 crewmate)。该评论表示:玩家 ii 声称玩家 jj 的身份是 cc。

  • 冒险者(imposter)总是说谎;
  • 船员(crewmate)总是说真话。

请帮助西奥法尼斯求出其余塞浦路斯玩家中可能的最大冒险者人数;若所有评论相互矛盾,则判定为不可能(详见注释说明)。

注意:每位玩家有且仅有一种身份,即要么是冒险者(imposter),要么是船员(crewmate)。

输入格式

The first line contains a single integer tt (1≤t≤1041 \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 nn and mm (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5; 0≤m≤5⋅1050 \le m \le 5 \cdot 10^5) — the number of players except Theofanis and the number of comments.

Each of the next mm lines contains a comment made by the players of the structure "ii jj cc" where ii and jj are two distinct integers and cc is a string (1≤i,j≤n1 \le i, j \le n; i≠ji \neq j; cc is either imposter or crewmate).

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

It is guaranteed that the sum of all nn does not exceed 2⋅1052 \cdot 10^5 and the sum of all mm does not exceed 5⋅1055 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——测试用例的数量。随后是每个测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5;0≤m≤5⋅1050 \le m \le 5 \cdot 10^5)——分别表示除 Theofanis 外的玩家数量和评论数量。

接下来的 mm 行中,每行包含一条由玩家发布的评论,格式为 "ii jj cc",其中 ii 和 jj 是两个不同的整数,cc 是一个字符串(1≤i,j≤n1 \le i, j \le n;i≠ji \neq j;cc 要么是 imposter,要么是 crewmate)。

同一对 (i,j)(i, j) 可能有多条评论。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5,且所有测试用例中 mm 的总和不超过 5⋅1055 \cdot 10^5。

输出格式

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

对于每个测试用例,输出一个整数——最多可能的冒名顶替者数量。如果评论相互矛盾,则输出 −1-1。

输入输出样例

  • 输入#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

    输出#1

    2
    4
    -1
    2
    5

说明/提示

In the first test case, imposters can be Andreas 22 and 33.

In the second test case, imposters can be Andreas 11, 22, 33 and 55.

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

在第一个测试用例中,冒名顶替者可以是玩家 22 和 33。

在第二个测试用例中,冒名顶替者可以是玩家 11、22、33 和 55。

在第三个测试用例中,发言内容相互矛盾。这是因为玩家 11 称玩家 22 是冒名顶替者,而玩家 22 称玩家 11 是船员。若玩家 11 是船员,则他必定说真话,因此玩家 22 必定是冒名顶替者;但若玩家 22 是冒名顶替者,则他必定在说谎,因此玩家 11 不可能是船员。矛盾。

输入解题思路,AI测评打分。不知道怎么写?

首页