Explanation
Constraint Propagation
Suppose the value of one endpoint of an edge is . If , then we can find the value of the other endpoint to be . Based on this, if we fix one node's value in a connected component, all other values in that component are determined by following the edges.
Determining General Values of Nodes
For each connected component, we can express all node values in terms of a single variable :
- We start with an arbitrary node and set its value to
- Then we propagate through the edges. If and edge has sum , then
This means that every node's value is represented in the form of , where .
Checking Feasibility in Cyclic Connected Components
We encounter a cycle when we visit an already-visited node. This means that we need to check for feasibility.
- If we reach node again, we have two expressions for it
- The cycle gives us an equation in terms of
- If the equation has no solution or contradicts a previous solution, the problem is infeasible
Why Cycle Equations Determine x
Acyclic Connected Components
When no cycle determines , we must choose its value to minimize the expression: . This can be done by setting to the median of the roots, as this problem reduces down to the minimization of the sum of absolute differences.
Why Median Minimizes Sum
Determining Actual Values of Nodes
Regardless of how we computed , once we have it, we need to propagate this value throughout the connected components. We can plug into each and determine the exact value of each node that minimizes the sum of their absolute values.
Implementation
Time Complexity:
C++
#include <algorithm>#include <iomanip>#include <iostream>#include <vector>using namespace std;using ll = long long;using pll = pair<ll, ll>;const ll INF = 1e18;
Java
import java.io.*;import java.util.*;public class Graph {static final long INF = (long)1e18;static List<List<Pair>> adj;static List<Pair> coeffs; // node value represented as ax + b (stored as {a, b})static boolean[] vis;static double[] ans;
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!