/ * Analog bipartite graph: each point as an edge, is connected to a column and a row (abstracted as a point, are listed in the left row and right) obtained from the meaning of the questions ab connected, ac coupled, bd connected, then the dc do not then connected to the number at the plus side is equivalent to a bipartite graph Unicom needs into a disjoint-set can be solved * / #include <bits / STDC ++ H.> the using namespace STD; #define MAXN 400005 int F. [MAXN], n- , m, Q; int Find ( int X) { return F. [X] X X ==:? F. [X] = Find (F. [X]); } void Bing ( int A, int B) { int T1 = Find (A), T2 = Find (B); IF (T1 =! T2) F. [T1] = T2; } int main(){ cin>>n>>m>>q; for(int i=1;i<=n+m;i++)F[i]=i; while(q--){ int u,v; cin>>u>>v; v+=n; bing(u,v); } int ans=0; for(int i=1;i<=n+m;i++) if(find(F[i])==i)ans++; cout<<ans-1; }