蒟蒻求条,玄关
  • 板块P2078 朋友
  • 楼主xcms
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/6/5 18:24
  • 上次更新2024/6/5 20:55:30
查看原帖
蒟蒻求条,玄关
1245940
xcms楼主2024/6/5 18:24

我滴代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+7;
int x1[N],ya[N];
int x2[N],y2[N];
int f1[N],f2[N];
int dfs1(int x){
	if(f1[x]!=x)f1[x]=dfs1(f1[x]);
	return f1[x];
}
int dfs2(int x){
	if(f2[x]!=x)f2[x]=dfs2(f2[x]);
	return f2[x];	
}
signed main(void){
	int n,m,p,q;
	cin>>n>>m>>p>>q;
	for(int i=1;i<=p;i++)cin>>x1[i]>>ya[i];
	for(int i=1;i<=q;i++)cin>>x2[i]>>y2[i];
	for(int i=1;i<=q;i++)x2[i]=abs(x2[i]),y2[i]=abs(y2[i]);
	for(int i=1;i<=n;i++)f1[i]=i;
	for(int i=1;i<=m;i++)f2[i]=i;
	for(int i=1;i<=p;i++){
		int x=dfs1(x1[i]);
		int y=dfs1(ya[i]);
		if(y>=x)swap(x,y);
		if(x!=y)f1[x]=y;
	}
	for(int i=1;i<=q;i++){
		int x=dfs2(x2[i]);
		int y=dfs2(y2[i]);
		if(y>=x)swap(x,y);
		if(x!=y)f2[x]=y;
	} 
	int cnt1=0,cnt2=0;
	for(int i=1;i<=p;i++){
		if(dfs1(i)==1)cnt1++;
	} 
	for(int i=1;i<=q;i++){
		if(dfs2(i)==1)cnt2++;
	}
	cout<<min(cnt1,cnt2);
	return 0;
}
2024/6/5 18:24
加载中...