POJ 1861 Network Krusakl模板题 最小生成树

!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!!!!例子错了!!!

简直污

Sample Output

1
4
1 2
1 3
2 3
3 4
这是错的

正确应该是

Sample Output

1
3
1 2
1 3
3 4
ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 15010
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
struct Edge{
    int u,v,w;
    Edge(){}
    Edge(int _u,int _v,int _w):u(_u),v(_v),w(_w){}
    friend bool operator<(const Edge a,const Edge b){
        return a.w<b.w;
    }
}my[maxn];
int fa[maxn],n,m;
int find_fa(int x){
    return x==fa[x]?x:(fa[x]=find_fa(fa[x]));
}
void krusakl(){
    int aa[maxn],bb[maxn];
    sort(my,my+m);
    int x,y,maxx=0;
    int cnt=0;///记录加入的边
    FOR(i,0,m-1){
        x=find_fa(my[i].u);
        y=find_fa(my[i].v);
        if(x!=y){
            fa[y]=x;
            aa[cnt]=my[i].u;
            bb[cnt]=my[i].v;
            cnt++;
            maxx=maxx>my[i].w?maxx:my[i].w;
        }
        if(cnt>=n-1)break;
    }
    printf("%d\n%d\n",maxx,n-1);
    FOR(i,0,cnt-1)printf("%d %d\n",aa[i],bb[i]);
}
int main(){
    while(rd2(n,m)!=EOF){
        int u,v,w;
        FOR(i,0,m)fa[i]=i;
        FOR(i,0,m-1){
            rd2(u,v);
            rd(w);
            my[i]=Edge(u,v,w);
        }
        krusakl();
    }
    return 0;
}
/*
4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值