Explanation
If we only have two players and , then we have 4 cases.
If vouches for and is an imposter, then must also be an imposter.
If vouches for and is a crewmate, then must also be a crewmate.
If accuses of being an imposter and is an imposter, then must be a crewmate.
If accuses of being an imposter and is a crewmate, then must be an imposter.
You'll notice that whenever accuses , they'll always be of a different type, and when vouches for , 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:
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!