蒟蒻63分求助

P3366 【模板】最小生成树

Dino_chx @ 2022-08-23 13:19:41

蒟蒻63分 #8 #9 #10 #13 WA

向大佬们求助

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e4+10;
int fa[1000000];
int n,m,cnt;
long long ans;
struct little_tree
{
int x,y;
int len;
}p[N];
inline int read()
{
register int f=1,k=0;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
k=k*10+ch-'0';
ch=getchar();
}
return f*k;
}
int find(int f)
{
if(fa[f]==f)
return f;
else
return fa[f]=find(fa[f]);
}
inline bool cmp(little_tree a,little_tree b)
{
return a.len<b.len;
}
void kr()
{
sort(p,p+m,cmp);
for(register int i=0;i<m;i++)
{
int xx=find(p[i].x),yy=find(p[i].y);
if(xx==yy)
continue;
ans+=p[i].len;
fa[xx]=yy;
cnt++;
if(cnt==n-1)
break;
}
return;
}
int main()
{
n=read();
m=read();
for(register int i=1;i<=n;i++)
{
fa[i]=i;
}
for(register int i=0;i<m;i++)
{
p[i].x=read();
p[i].y=read();
p[i].len=read();
}
kr();
if(ans>0)
printf("%d",ans);
else
printf("orz");
return 0;
}

by EastPorridge @ 2022-08-23 13:27:12

@clancysam 存边的数组开小了;;


by Dino_chx @ 2022-08-23 13:34:18

@EastPorridge前面的问题解决了!

蒟蒻不知道为什么#13爆WA耶?


by TheSky233 @ 2022-08-23 13:35:00

输出 ans 的条件是 cnt=n-1 而不是 ans>0


by Dino_chx @ 2022-08-23 13:55:13

AC了,谢谢

此贴完结


|