2026/5/10晚上 模拟赛总结
第1题 【模板】最小生成树
100分
用一个简单的并查集求最小生成树
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+50;
int n,m,ans,f[N];
struct E{
int x,y,w;
}e[N];
int find(int x){
if(f[x]==x)return x;
return f[x]=find(f[x]);
}
void ch(int x,int y){
x=find(x);
y=find(y);
if(x!=y)f[x]=y;
}
int cmp(E x,E y){
return x.w<y.w;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=m;i++){
cin>>e[i].x>>e[i].y>>e[i].w;
}
sort(e+1,e+1+m,cmp);
for(int i=1,cnt=0;i<=m;i++){
if(find(e[i].x)!=find(e[i].y)){
cnt++;
ans+=e[i].w;
ch(e[i].x,e[i].y);
if(cnt==n-1){
cout<<ans;
return 0;
}
}
}
cout<<"orz";
return 0;
}
第2题 P4826 [USACO15FEB] Superbull S
100分
只比上一题多一个存边的步骤,因
#include<bits/stdc++.h>
using namespace std;
const int N=4e6+50;
long long n,m,ans,a[N],f[N];
struct E{
int x,y,w;
}e[N];
int find(int x){
if(f[x]==x)return x;
return f[x]=find(f[x]);
}
void ch(int x,int y){
x=find(x);
y=find(y);
if(x!=y)f[x]=y;
}
int cmp(E x,E y){
return x.w>y.w;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
f[i]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
e[++m]={i,j,(a[i]^a[j])};
}
}
sort(e+1,e+1+m,cmp);
for(int i=1,cnt=0;i<=m;i++){
if(find(e[i].x)!=find(e[i].y)){
ch(e[i].x,e[i].y);
cnt++;
ans+=e[i].w;
if(cnt==n-1){
cout<<ans;
return 0;
}
}
}
return 0;
}
第3题 AT_abc399_c [ABC399C] Make it Forest
100分
直接判断环
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+50;
int n,m,ans,f[N];
int find(int x){
if(f[x]==x)return x;
return f[x]=find(f[x]);
}
void ch(int x,int y){
x=find(x);
y=find(y);
if(x!=y)f[x]=y;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1,x,y;i<=m;i++){
cin>>x>>y;
if(find(x)==find(y))ans++;
else ch(x,y);
}
cout<<ans;
return 0;
}
第4题 P8074 [COCI 2009/2010 #7] SVEMIR
0分
哎!我掉进了内存设下的坑了
const int N=6e6+50;
我只要把
N 开成原来的3/1 ,就可以得35 分
满分做法
其实直接存储每个与相邻的点的边即可
- 开long long
- 按x从小到大排序 把相邻点连边
- 按y从小到大排序 把相邻点连边
- 按z从小到大排序 把相邻点连边
- 最小生成树
#include<bits/stdc++.h> #define int long long using namespace std; const int N=2e6+50; int n,m,f[N],ans; struct E{ int x,y,w; }e[N]; struct V{ int x,y,z,i; }a[N]; int find(int x){ if(f[x]==x)return x; return f[x]=find(f[x]); } void ch(int x,int y){ x=find(x); y=find(y); if(x!=y)f[x]=y; } int cmp(V x,V y){ return x.x<y.x; } int cmp1(V x,V y){ return x.y<y.y; } int cmp2(V x,V y){ return x.z<y.z; } int cmp3(E x,E y){ return x.w<y.w; } signed main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i].x>>a[i].y>>a[i].z; a[i].i=i; f[i]=i; } sort(a+1,a+1+n,cmp); for(int i=2;i<=n;i++){ e[++m]={a[i].i,a[i-1].i,abs(a[i].x-a[i-1].x)}; } sort(a+1,a+1+n,cmp1); for(int i=2;i<=n;i++){ e[++m]={a[i].i,a[i-1].i,abs(a[i].y-a[i-1].y)}; } sort(a+1,a+1+n,cmp2); for(int i=2;i<=n;i++){ e[++m]={a[i].i,a[i-1].i,abs(a[i].z-a[i-1].z)}; } sort(e+1,e+1+m,cmp3); for(int i=1,cnt=0;i<=m;i++){ if(find(e[i].x)!=find(e[i].y)){ ch(e[i].x,e[i].y); cnt++; ans+=e[i].w; if(cnt==n-1){ cout<<ans; return 0; } } } return 0; }
第5题 AT_abc364_f [ABC364F] Range Connect MST
30分
满分做法
- 开long long
- 按照c[i]从小到大排序操作
- 无论如何n+i都需要l[i]连接
- 存储这个连通块下一个没有链接的位置
- 最小生成树
#include<bits/stdc++.h> #define int long long using namespace std; const int N=4e5+50; int n,f[N],ans,q; struct E{ int x,y,w; }a[N]; int find(int x){ if(f[x]==x)return x; return f[x]=find(f[x]); } void ch(int x,int y){ x=find(x); y=find(y); if(x!=y)f[x]=y; } int cmp(E x,E y){ return x.w<y.w; } signed main(){ cin>>n>>q; for(int i=1;i<=n+q;i++){ f[i]=i; } for(int i=1;i<=q;i++){ cin>>a[i].x>>a[i].y>>a[i].w; } sort(a+1,a+1+q,cmp); for(int i=1,cnt=0;i<=q;i++){ ch(i+n,a[i].x); ans+=a[i].w; int l=find(a[i].x),r=find(a[i].y); while(l!=r){ ans+=a[i].w; ch(l,l+1); l=find(l); } } for(int i=1;i<=n+q;i++){ if(find(i)!=find(1)){ cout<<-1; exit(0); } } cout<<ans; return 0; }