< Topic link >
Title effect:
Given a matrix of n * m grid, to which point, four rectangular points if there are three points for all elements in a composition, then the fourth point will automatically generate new elements. I ask you how many points together at least be able to fill the grid.
Problem-solving analysis:
not very good thought, as the ranks of the first two sets, each add operation, the equivalent of a set of x to y even a set of edges.
This problem can be transformed into ingenious, x and y to make the set of all points to add a minimum of communication with many sides. The initial situation requires n + m-1 edges (because there are two sets of n + m points, and the same set of edges not connected).
The final number is then determined in accordance with the block-side communication subject has been given, with disjoint-set of judging communication. The main idea is that this transformation is not good think.
#include <bits/stdc++.h> using namespace std; template<typename T> inline void read(T&x){ x=0;int f=1;char c=getchar(); while(c<'0'||c>'9'){ if(c=='-')f=-1;c=getchar(); } while(c>='0'&&c<='9'){ x=x*10+c-'0';c=getchar(); } x*=f; } #define REP(i,s,t) for(int i=s;i<=t;i++) const int N = 2e5+5; int fa[N<<1]; int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]); } inline bool merge(int x,int y){ int f1=find(x),f2=find(y); if(f1!=f2){ fa[f1]=f2;return true;} return to false ; } int main () { int n-, m, Q; Read (n-); Read (m); Read (Q); REP (I, 0 , n-+ m) FA [I] = I; int ANS n-M- + = . 1 ; // actually use the minimum number of sides, such that n + m communication points, simply use up to n + m-1 pieces REP (I, . 1 , Q) { int A, B; Read (a); Read (B); B + = n-; IF (Merge (a, B)) ans--; // if this edge is valid, then the number of edges required - } COUT << << ANS endl; }