https://www.luogu.com.cn/problem/P2330
题设就已经吧最小生成树的思想写出来了
其实就是个贪心
一遍过
#include<bits/stdc++.h>
using namespace std;
#define in Read()
typedef long long ll;
int in{
int i=0,f=1; char ch=0;
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') f=-1, ch=getchar();
while('0'<=ch&&ch<='9') i=(i<<1)+(i<<3)+ch-48, ch=getchar();
return i*f;
}
const int N=1e5+5;
struct edge{
int u,v,c;
edge(){};
edge(int U,int V,int C){ u=U, v=V, c=C;}
}e[N];
int n,m,cnt,fa[N];
bool cmp(const edge &a, const edge &b){return a.c<b.c;}
int get(int x){return fa[x]==x?x:fa[x]=get(fa[x]);}
int main(){
// freopen("1.in","r",stdin);
n=in,m=in;
for(int i=1;i<=m;++i){
int u=in,v=in,c=in;
e[++cnt]=edge(u,v,c);
}
cnt=0;
sort(e+1,e+m+1,cmp);
for(int i=1;i<=n;++i) fa[i]=i;
for(int i=1;i<=m;++i){
int u=get(e[i].u), v=get(e[i].v);
if(u==v) continue;
++cnt;
fa[u]=v;
if(cnt==n-1){
printf("%d %d\n",cnt,e[i].c);
return 0;
}
}
}
标签:ch,19,最小,ce,int,long,getchar
From: https://www.cnblogs.com/antimony-51/p/16993046.html