The Number of Imposters (Codeforces 1594D)
Tags: Others, dsu

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.

Sample Inputs:
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

Sample Outputs:
2
4
-1
2
5

Solution (Python):
import sys

def solve():
    inp = sys.stdin.readline
    (n, m) = map(int, inp().split())
    g = [[] for i in range(n + 1)]
    for i in range(m):
        (u, v, c) = inp().split()
        (u, v) = map(int, (u, v))
        if c == 'crewmate':
            g[u].append((v, 0))
            g[v].append((u, 0))
        else:
            g[u].append((v, 1))
            g[v].append((u, 1))
    val = [None] * (n + 1)
    r = 0
    for i in range(1, n + 1):
        if val[i] is None:
            q = [i]
            val[i] = 0
            i = 0
            c = [1, 0]
            while i < len(q):
                u = q[i]
                i += 1
                for (v, w) in g[u]:
                    if val[v] is None:
                        val[v] = val[u] ^ w
                        c[val[v]] += 1
                        q.append(v)
                    elif val[v] != val[u] ^ w:
                        print(-1)
                        return
            r += max(c)
    print(r)

def main():
    for i in range(int(sys.stdin.readline())):
        solve()
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

def solve():
    (n, m) = map(int, input().split())
    graph = [[] for _ in range(n + 1)]
    for i in range(m):
        (u, v, c) = input().split()
        (u, v) = map(int, (u, v))
        if c == 'crewmate':
            graph[u].append((v, 0))
            graph[v].append((u, 0))
        else:
            graph[u].append((v, 1))
            graph[v].append((u, 1))
    val = [None] * (n + 1)
    ans = 0
    for i in range(1, n + 1):
        if val[i] is None:
            q = [i]
            val[i] = 0
            i = 0
            c = [1, 0]
            while i < len(q):
                u = q[i]
                i += 1
                for (v, w) in graph[u]:
                    if val[v] is None:
                        val[v] = val[u] ^ w
                        c[val[v]] += 1
                        q.append(v)
                    elif val[v] != val[u] ^ w:
                        return -1
            ans += max(c)
    return ans
for _ in range(int(input())):
    print(solve())

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    (n, m) = map(int, input().split())
    e = [[] for _ in range(n + 1)]
    for _ in range(m):
        (u, v, r) = input().split()
        u = int(u)
        v = int(v)
        e[u].append((v, r == 'imposter'))
        e[v].append((u, r == 'imposter'))
    color = [-1] * (n + 1)
    vis = [0] * (n + 1)
    ng = 0
    for i in range(1, n + 1):
        if vis[i]:
            continue
        s = [i]
        vis[i] = 1
        color[i] = 0
        newvis = set()
        while len(s):
            x = s.pop()
            newvis.add(x)
            for (y, r) in e[x]:
                if color[y] != -1 and color[x] ^ r != color[y]:
                    ng = 1
                    break
                if vis[y]:
                    continue
                vis[y] = 1
                s.append(y)
                color[y] = color[x] ^ r
        cr = 0
        im = 0
        for x in newvis:
            if color[x]:
                im += 1
            else:
                cr += 1
        if im < cr:
            for x in newvis:
                color[x] ^= 1
    if ng == 0:
        print(color.count(1))
    else:
        print(-1)