Zoj 1542 [Kruskal algorithm]

Source: Internet
Author: User

The sample of zoj is incorrect. The problem is to find the Minimum Spanning Tree and the longest path.

#include <vector>#include <list>#include <map>#include <set>#include <string.h>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;#define LL long long#define pi acos(-1)#define N 15010#define INF 999999999#define eps 1e-8//****************************************//zoj 1542 Network//Copyright@leolin. All rights reserved.//****************************************struct node{    int x,y,len;}e[N],hub[N];int fa[1100];int n,m;bool cmp(node a,node b){    return a.len<b.len;}int find(int x){    while(x!=fa[x])        x=fa[x];    return x;}void krasual(){    int i,j,k;    for(i=0;i<=n;i++)        fa[i]=i;    sort(e+1,e+m+1,cmp);    int max=0;    int cnt=0;    for(i=1;i<=m;i++)    {        int a=find(e[i].x);        int b=find(e[i].y);        if(a!=b)        {            fa[a]=b;            if(max<e[i].len)max=e[i].len;            hub[cnt++]=e[i];        }    }    printf("%d\n%d\n",max,cnt);    for(i=0;i<cnt;i++)    printf("%d %d\n",hub[i].x,hub[i].y);}int main(){//freopen("a.txt","r",stdin);    while(scanf("%d%d",&n,&m)!=EOF)    {        for(int i=1;i<=m;i++)            scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].len);            krasual();    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.