1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include<iostream> #include<cstdio> #include<stack> #include<queue> #include<deque> #include<vector> #include<cstdlib> #include<string> #include<cstring> #include<algorithm> #include<ctime> #include<bitset> #include<set> #include<cmath> using namespace std;
#define rint register int #define Int64 long long #define max(x,y) (((x)>(y))?(x):(y)) #define min(x,y) (((x)<(y))?(x):(y)) #define clr(x,y) memset(x,y,sizeof(x)) #define sqar(x) (x)*(x) #define lowbit(x) (x&(-x)) #define swp(x,y) x^=y,y^=x,x^=y #define Maxn 5005 #define Maxm 200005
class DSU{ private: int belong[Maxn]; public: void init(){for(int i=0;i<Maxn;i++)belong[i]=i;} int find(int u){ if(u==belong[u])return u; return belong[u]=find(belong[u]); } bool islink(int u,int v){ int fu=find(u); int fv=find(v); return fu==fv; } void link(int u,int v){ int fu=find(u); int fv=find(v); belong[fu]=fv; } }D;
struct edge{ int u; int v; int val;
bool operator <(const edge& cmp)const{ return val<cmp.val; } }E[Maxm];
int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif D.init(); int N,M;scanf("%d%d",&N,&M); for(int i=1;i<=M;i++){ int u,v,z;scanf("%d%d%d",&u,&v,&z); E[i]={u,v,z}; } sort(E+1,E+1+M); int tot=0,now=1,ans=0; while(now<=M){ int u=E[now].u; int v=E[now].v; if(D.islink(u,v)==false){ D.link(u,v); tot++; ans+=E[now].val; } if(tot==N-1)break; now++; } if(tot!=N-1)printf("orz"); else printf("%d",ans); return 0;
}
|