Table of Contents

ExplanationImplementation

Explanation

If we only have two players xx and yy, then we have 4 cases.

  • If xx vouches for yy and xx is an imposter, then yy must also be an imposter.

  • If xx vouches for yy and xx is a crewmate, then yy must also be a crewmate.

  • If xx accuses yy of being an imposter and xx is an imposter, then yy must be a crewmate.

  • If xx accuses yy of being an imposter and xx is a crewmate, then yy must be an imposter.

You'll notice that whenever xx accuses yy, they'll always be of a different type, and when xx vouches for yy, they'll always be of the same type.

With this information, we can build a bipartite graph and color nodes with the same color if they vouch for each other, or different colors if one of them accuses the other. If we can't build this graph, then no valid arrangement can exist.

Otherwise, we'll consider each connected component independently and keep track of the number of crewmates and imposters. At the end, we'll return the sum of the maximum of these two values for all connected components. We don't care about the roles of each node because we can switch all of them to get another valid solution.

Implementation

Time Complexity: O(N)\mathcal{O}(N)

C++

#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main() {
int t;
cin >> t;
for (int tc = 0; tc < t; tc++) {

Java

import java.io.*;
import java.util.*;
public class AmongUs {
private static class Edge {
public int first;
public boolean second;
public Edge(int first, boolean second) {
this.first = first;

Python

for _ in range(int(input())):
n, q = map(int, input().split())
"""
adj[i][j] = {the jth adjacent node from i,
their statement (0 = imposter, 1 = crewmate)}
"""
adj = [[] for _ in range(n)]
for _ in range(q):

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!