Hint 1
Hint 2
Explanation
We can treat each set as a graph with several connected components. For each , if exists, then and are in the same connected component inside graph , and similarly for . Each number in the connected component must belong to one set. Therefore, our condradiction comes from if there is a number in the component such that only exists, and another in the component such that only exists, or vice cersa.
We can check each component for the contradiction using DSU. The status of the whole component will be the intersection of what graph each number in the component can reside in.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;Code Snippet: DSU (from the module) (Click to expand)int main() {int n, a, b;cin >> n >> a >> b;vector<int> p(n);for (int i = 0; i < n; i++) { cin >> p[i]; }
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!