暑假算法训练day7(并查集)

115 篇文章 3 订阅

并查集过的比较顺利

A - 并查集

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1e4+10;
int f[N];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int a,int b)
{
	int x=find(a),y=find(b);
	if(x!=y)f[y]=x;
}
void init()
{
	for(int i=0;i<=n;i++)f[i]=i;
}
void solve()
{
	cin>>n>>m;
	init();
	while(m--)
	{
		int z;
		cin>>z;
		if(z==1)
		{
			int x,y;
			cin>>x>>y;
			unite(x,y);
		}
		else
		{
			int x,y;
			cin>>x>>y;
			if(find(x)==find(y))
				cout<<"Y"<<endl;
			else cout<<"N"<<endl;
		}
	}
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

B - 家谱

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1e5+10;
int f[N];
map<string,int>mp1;
map<int,string>mp2;
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)f[b]=a;
}
void solve()
{
	string s;
	string fa;
	int idx=1;
	for(int i=1;i<N;i++)f[i]=i;
	while(cin>>s,s!="$")
	{
		if(s[0]=='#')
		{
			s=s.substr(1);
			if(!mp1.count(s))mp1[s]=idx,mp2[idx]=s,idx++;
			fa=s;
		}
		else if(s[0]=='+')
		{
			s=s.substr(1);
			if(!mp1.count(s))mp1[s]=idx,mp2[idx]=s,idx++;
			unite(mp1[fa],mp1[s]);
		}
		else
		{
			s=s.substr(1);
			cout<<s<<' '<<mp2[find(mp1[s])]<<endl;
		}
	}
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

C - 村村通

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1010;
int f[N];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)f[b]=a;
}
void solve()
{
	while(cin>>n>>m)
	{
	
		for(int i=0;i<N;i++)f[i]=i;
		for(int i=1;i<=m;i++)
		{
			int x,y;
			cin>>x>>y;
			unite(x,y);
		}
		int res=0;
		for(int i=1;i<=n;i++)
			if(f[i]==i)res++;
		cout<<res-1<<endl;
	}
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

D - 朋友

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m,p,q;
const int N=500010;
int f[N];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)
		f[b]=a;
}
void solve()
{
	cin>>n>>m>>p>>q;
	for(int i=1;i<N;i++)f[i]=i;
	for(int i=1;i<=p;i++)
	{
		int x,y;
		cin>>x>>y;
		unite(x,y);
	}
	for(int i=1;i<=q;i++)
	{
		int x,y;
		cin>>x>>y;
		x=-x+n;
		y=-y+n;
		unite(x,y);
	}
	int res1=0,res2=0;
	for(int i=1;i<=n;i++)if(find(i)==find(1))res1++;
	for(int i=n+1;i<=n+m;i++)if(find(i)==find(n+1))res2++;
	cout<<min(res1,res2)<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

E - Cthulhu

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=110,M=N*N;
int f[N];
struct node
{
	int a,b;
}e[M];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)f[b]=a;
}
void solve()
{
	cin>>n>>m;
	if(n!=m)
	{
		cout<<"NO"<<endl;
		return;
	}
	for(int i=1;i<=n;i++)f[i]=i;
	for(int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		e[i]={a,b};
	}
	int res=0;
	for(int i=0;i<m;i++)
	{
		int a=find(e[i].a),b=find(e[i].b);
		if(a!=b)unite(a,b);
		else
		{
			res++;
		}
	}
	if(res==1)cout<<"FHTAGN!"<<endl;
	else cout<<"NO"<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

F - DZY Loves Chemistry

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
int n,m;
const int N=10010;
bool st[N];
vector<int>v[N];
int res=1;
int dfs(int u)
{
	st[u]=true;
	
	for(int i=0;i<v[u].size();i++)
	{
		if(!st[v[u][i]])
		{
			res*=2;
			dfs(v[u][i]);
		}
	}
	return res;
}
signed main()
{
	io;
	cin>>n>>m;
	while(m--)
	{
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	int maxv=0;
	for(int i=1;i<=n;i++)
	{
		if(!st[i])maxv=max(maxv,dfs(i));
	}
	cout<<maxv<<endl;
	return 0;
}

G - Mocha and Diana (Easy Version)

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m1,m2;
const int N=2e5+10;
int f1[N],f2[N];
int find1(int x)
{
	if(x!=f1[x])f1[x]=find1(f1[x]);
	return f1[x];
}
int find2(int x)
{
	if(x!=f2[x])f2[x]=find2(f2[x]);
	return f2[x];
}
void unite1(int x,int y)
{
	int a=find1(x),b=find1(y);
	if(a!=b)f1[b]=a;
}
void unite2(int x,int y)
{
	int a=find2(x),b=find2(y);
	if(a!=b)f2[b]=a;
}
void solve()
{
	cin>>n>>m1>>m2;
	for(int i=0;i<N;i++)f1[i]=f2[i]=i;
	for(int i=1;i<=m1;i++)
	{
		int a,b;
		cin>>a>>b;
		unite1(a,b);
	}
	for(int i=1;i<=m2;i++)
	{
		int a,b;
		cin>>a>>b;
		unite2(a,b);
	}
	vector<PII>v;
	for(int i=1;i<=n;i++)
	{
		for(int j=i+1;j<=n;j++)
		{
			int a1=find1(i),b1=find1(j),a2=find2(i),b2=find2(j);
			if(a1!=b1&&a2!=b2)
			{
				v.pb({i,j});
				unite1(a1,b1);
				unite2(a2,b2);
			}
		}
	}
	cout<<v.size()<<endl;
	for(auto x:v)cout<<x.x<<' '<<x.y<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

H - Cow and Snacks

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1e5+10;
int f[N];
struct node
{
	int a,b;
}e[N];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)f[b]=a;
}
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)f[i]=i;
	for(int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		e[i]={a,b};
	}
	int res=0;
	for(int i=0;i<m;i++)
	{
		int a=find(e[i].a),b=find(e[i].b);
		if(a!=b)unite(a,b),res++;
	}
	cout<<m-res<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

I - Social Network

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=1e3+10;
int f[N],cnt[N];
int res;
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)
	{
		cnt[a]+=cnt[b];
		f[b]=a;
	}
	else res++;
}
void solve()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)f[i]=i,cnt[i]=1;
	for(int i=1;i<=m;i++)
	{
		int a,b;
		cin>>a>>b;
		unite(a,b);
		vector<int>v;
		for(int j=1;j<=n;j++)
		{
			if(f[j]==j)v.pb(cnt[j]);
		}
		sort(all(v),greater<int>());
		int ans=0;
		for(int j=0;j<v.size()&&j<=res;j++)
			ans+=v[j];
		ans--;
		cout<<ans<<endl;
	}
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}

J. pSort

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
#define x first
#define y second
#define LL long long 
#define int LL
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define PII pair<int,int>
#define ll_INF 0x7f7f7f7f7f7f7f7f
#define INF 0x3f3f3f3f
#define debug(x) cerr << #x << ": " << x << endl
#define io ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
LL Mod(LL a,LL mod){return (a%mod+mod)%mod;}
LL lowbit(LL x){return x&-x;}//最低位1及其后面的0构成的数值
LL qmi(LL a,LL b,LL mod) {LL ans = 1; while(b){ if(b & 1) ans = ans * (a % mod) % mod; a = a % mod * (a % mod) % mod; b >>= 1;} return ans; }
int _;
int n,m;
const int N=110;
int f[N];
int a[N],b[N];
int find(int x)
{
	if(x!=f[x])f[x]=find(f[x]);
	return f[x];
}
void unite(int x,int y)
{
	int a=find(x),b=find(y);
	if(a!=b)f[b]=a;
}
bool check(int x,int y)
{
	int a=find(x),b=find(y);
	return a==b;
}
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)f[i]=i;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>b[i];
	for(int i=1;i<=n;i++)
	{
		if(i-b[i]>=1)unite(i-b[i],i);
		if(i+b[i]<=n)unite(i+b[i],i);
	}
	for(int i=1;i<=n;i++)
	{
		if(!check(i,a[i]))
		{
			cout<<"NO"<<endl;
			return;
		}
	}
	cout<<"YES"<<endl;
}
signed main()
{
    io;
 //	cin>>_; 
//	while(_--)
    solve();
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值