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
%d
",maxx,n-1); FOR(i,0,cnt-1)printf("%d %d
",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 */