跳到正文

个人记录

4.19 -【生成树】最小生成树基础

Void_Rift约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年4月28日
更新
2026年4月28日
篇幅
约 11 分钟

版本

  1. v1当前
生成树 -【最小生成树与瓶颈生成树的概念】

课堂练习

-P3366 【模板】最小生成树-

思路

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10;
struct node
{
    int u,v,w;
}s[N];
int n,m,k,p[N];
int find(int x)
{
	return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
	int t1=find(x),t2=find(y);
	if(t1!=t2) p[t2]=t1;
}
bool cmp(node x,node y)
{
	return x.w<y.w;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=m;i++) cin>>s[i].u>>s[i].v>>s[i].w;
	sort(s+1,s+m+1,cmp);
	int cnt=0,ans=0;
	for(int i=1;i<=m;i++)
	{
		int u=s[i].u,v=s[i].v,w=s[i].w;
		if(find(u)!=find(v))
		{
			merge(u,v);
			cnt++;
			ans+=w;
			if(cnt==n-1)
			{
				cout<<ans;
				return 0;
			}
		}
	}
	cout<<"orz";
    return 0;
}

-P1195 口袋的天空-

思路

  • 跟板子差不多
  • 初始时 nn 个节点是 nn 个独立的联通块,每选一条不形成环的边,联通块的数量就减 1;1;所以要得到 kk 个联通块,需要选 n−kn-k 条不形成环的边
  • 特判:
    • 若 n<k:n<k: 无法把 nn 个节点分成 kk 个每个至少 11 个节点的联通块,无解
    • 若 n==k:n==k: 每个节点单独作为一个棉花糖,不需要连任何边,代价为 00

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
struct node
{
    int u,v,w;
}s[N];
int n,m,k,p[N],cnt,ans;
int find(int x)
{
	return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
	int t1=find(x),t2=find(y);
	if(t1!=t2) p[t2]=t1;
}
bool cmp(node x,node y)
{
	return x.w<y.w;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    cin>>n>>m>>k;
    if(n<k)
    {
    	cout<<"No Answer";
    	return 0;
	}
	if(n==k)
	{
		cout<<"0";
		return 0;
	}
    for(int i=1;i<=n;i++) p[i]=i;
    for(int i=1;i<=m;i++) cin>>s[i].u>>s[i].v>>s[i].w;
	sort(s+1,s+m+1,cmp);
	for(int i=1;i<=m;i++)
	{
		int u=s[i].u,v=s[i].v,w=s[i].w;
		if(find(u)!=find(v))
		{
			merge(u,v);
			cnt++;
			ans+=w;
			if(cnt==n-k)
			{
				cout<<ans;
				return 0;
			}
		}
	}
	cout<<"No Answer";
    return 0;
}

-P4826 [USACO15FEB] Superbull S-

思路

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e7+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
struct node
{
    int u,v,w;
}s[N];
int n,m,k,p[N],a[N],cnt,ans;
int find(int x)
{
	return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
	int t1=find(x),t2=find(y);
	if(t1!=t2) p[t2]=t1;
}
bool cmp(node x,node y)
{
	return x.w>y.w;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
		p[i]=i;
        cin>>a[i];
	}
    for(int i=1;i<=n;i++)
    {
    	for(int j=i+1;j<=n;j++)
        {
            s[++m]={i,j,a[i]^a[j]};
        }
	}
	sort(s+1,s+m+1,cmp);
	for(int i=1;i<=m;i++)
	{
		int u=s[i].u,v=s[i].v,w=s[i].w;
		if(find(u)!=find(v))
		{
			merge(u,v);
			cnt++;
			ans+=w;
			if(cnt==n-1) break;
		}
	}
    cout<<ans;
    return 0;
}

-AT_abc328_e [ABC328E] Modulo MST-

思路

  • 上难度了,这题需要取模,Kruskal\text{Kruskal} 用不了了 ?这啥

我去太棒了我们有救了

  • 说实话要求和板子差不多
  • 直接 DFS\text{DFS} 遍历(选或不选)
  • 对于选好的 n−1n-1 条边,并查集判环:
    • 无环:合法生成树,计算总权 mod k\text{mod}\space k,并更新最小值
    • 有环:跳过
  • 最后输出总权即可

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f3f3f;
struct node
{
    int u,v,w;
}s[N];
int n,m,mod,res[N],p[N],ans;
int find(int x)
{
	return ((p[x]==x)? x:p[x]=find(p[x]));
}
void merge(int x,int y)
{
	int t1=find(x),t2=find(y);
	if(t1!=t2) p[t1]=t2;
}
bool check()
{
	for(int i=1;i<=n;i++) p[i]=i;
	for(int i=1;i<=n-1;i++)
	{
		int u=find(s[res[i]].u),v=find(s[res[i]].v);
		if(u!=v) merge(u,v);
		else return false;
	}
	return true;
}
void dfs(int cur,int sel,int sum)
{
	if(sel==n-1)
	{
		if(!check()) return ;
		ans=min(ans,sum%mod);
		return ;
	}
	if(cur>m) return ;
	res[sel+1]=cur;
	dfs(cur+1,sel+1,(sum%mod+s[cur].w%mod)%mod);
	dfs(cur+1,sel,sum%mod);
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n>>m>>mod;
    for(int i=1;i<=m;i++) cin>>s[i].u>>s[i].v>>s[i].w;
    dfs(1,0,0);
    cout<<ans;
    return 0;
}

-P5687 [CSP-S 2019 江西] 网格图-

思路

  • 网格太大了,Kruskal\text{Kruskal} 又用不了了

?

  • 因为 n,m≤3×105n,m\le3\times10^5,无法枚举所有边,需批量处理同权边
  • 先选全局最小的一整行横向边和一整列纵向边,保证初始无环
  • 每次选当前剩余最小的边,根据已选的行/列数,计算当前能批量加的边数(无环) 累计总代价和已选边数,直到选够 n×m−1n\times m-1条边,输出总代价即可

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
int n,m,x[N],y[N],posx=2,posy=2,line=1,row=1;
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>x[i];
    for(int i=1;i<=m;i++) cin>>y[i];
    sort(x+1,x+n+1);
    sort(y+1,y+m+1);
    int cnt=n-1+m-1,ans=(m-1)*x[1]+(n-1)*y[1];
    while(cnt!=n*m-1)
    {
    	if(x[posx]<y[posy])
    	{
    		int tmp=m-1-row+1;
    		ans+=x[posx]*tmp;
    		cnt+=tmp;
    		posx++;
    		line++;
		}
		else
		{
    		int tmp=n-1-line+1;
    		ans+=y[posy]*tmp;
    		cnt+=tmp;
    		posy++;
    		row++;
		}
	}
	cout<<ans;
    return 0;
}

齐芒nb

原文