Graph Two-Coloring
Author: Siyong Huang
Prerequisites
Introducing bipartite graphs.
Graph two-coloring refers to assigning a boolean value to each node of the graph, dictated by the edge configuration. The most common example of a two-colored graph is a bipartite graph, in which each edge connects two nodes of opposite colors.
Focus Problem – read through this problem before continuing!
Resources
Resources | |||
---|---|---|---|
CPH | Brief solution sketch with diagrams. |
Solution - Building Teams
The idea is that we can arbitrarily label a node and then run DFS. Every time we visit a new (unvisited) node, we set its color based on the edge rule. When we visit a previously visited node, check to see whether its color matches the edge rule.
C++
1#include <cstdio>2#include <vector>34const int MN = 1e5+10;56int N, M;7bool bad, vis[MN], group[MN];8std::vector<int> a[MN];910void dfs(int n=1, bool g=0)
Java
Warning!
Because Java is so slow, an adjacency list using lists/arraylists results in TLE. Instead, the Java sample code will use the Chinese edge representation.
1import java.io.*;2import java.util.*;34public class BuildingTeams5{6 static InputReader in = new InputReader(System.in);7 static PrintWriter out = new PrintWriter(System.out);89 public static final int MN = 100010;10 public static final int MM = 200010;
Problems
Status | Source | Problem Name | Difficulty | Tags | Solution | URL |
---|---|---|---|---|---|---|
CF | Easy | Show TagsBipartite | Check CF | |||
Silver | Easy | Show TagsBipartite | External Sol | |||
Baltic OI | Hard | Check CF | ||||
APIO | Very Hard |
Module Progress:
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!