|
- #include<iostream>
- using namespace std;
- int n,m,p,q;
- const int mx=10010;
- int par[mx][3],i;///par[mx][1]是男
- int getpar(int a,int x)
- {
- ///找根节点
- if (par[a][x]!=a)
- par[a][x]=getpar(par[a][x],x);
- return par[a][x];
- }
- void merge_(int a,int b,int x)
- {
- ///合并a,b所在集合
- par[getpar(a,x)][x]=getpar(b,x);
- return;
- }
- int main()
- {
- cin>>n>>m>>p>>q;
- for (i=1; i<=n; i++) par[i][1]=i;
- for (i=1; i<=m; i++) par[i][2]=i;
- for (i=1; i<=p; i++)
- {
- int a,b;
- cin>>a>>b;
- merge_(a,b,1);
- }
- for (i=1; i<=q; i++)
- {
- int a,b;
- cin>>a>>b;
- a=-a,b=-b;
- merge_(a,b,2);
- }
- int ansa=0,ansb=0;
- for (i=1; i<=n; i++)
- if (getpar(i,1)==getpar(1,1)) ansa++;
- for (i=1; i<=m; i++)
- if (getpar(i,2)==getpar(1,2)) ansb++;
- cout<<min(ansa,ansb);
- return 0;
- }
复制代码 |
|