GYM 100803F There is No alternative

Source: Internet
Author: User
GYM 100803F (CSU 2097) There is No alternative (enumeration + minimum spanning tree) Test Instructions

Find a few irreplaceable edges on the minimum spanning tree. Thinking of solving problems

The problem is to seek the cut on the smallest spanning tree. It is necessary to first find the minimum spanning tree, where we can write down the edges of the smallest spanning tree. Then let's see, we can enumerate the edges on each of the smallest spanning tree and make the minimum spanning tree, if it doesn't make up the smallest spanning tree or the smallest spanning tree is not equal to the original minimum spanning tree, then this edge is cut and the time complexity is O (N*MLOGM). Code

#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace
Std
#define MAXN 50005 int n,m,num,sum;
struct line {int a,b,val;} ARR[MAXN];
int Flag[maxn],side[505],p[505],ans; int CMP (line A,line b) {return a.val<b.val;} int find (int x) {return p[x]==x?x:p[x]=find (p[x]);} int Kruska
    L (int cur) {for (int i=0; i<=n; i++) p[i]=i;
    int cnt=0,su=0;
        for (int i=0; i<m; i++) {if (!flag[i]&&cur!=-1) continue;
        int Xx=find (ARR[I].A);
        int Yy=find (ARR[I].B);
            if (xx!=yy) {if (cur==-1) side[cnt]=i;
            P[xx]=yy;
            Su+=arr[i].val;
        cnt++;
    } if (cnt==n-1) return su;
} return-1;
    } int main () {//Freopen ("In.txt", "R", stdin);
    Ios::sync_with_stdio (FALSE); while (Cin>>n>>m) {for (int i=0; i<m; i++) CIN&GT;&GT;ARR[I].A&GT;&GT;ARR[I].B&GT;&G t;arr[I].val;
        for (int i=0; i<m; i++) flag[i]=1;
        Sort (arr,arr+m,cmp);
        Ans=kruskal (-1);
        num=sum=0;
            for (int i=0; i<n-1; i++) {flag[side[i]]=0;
            int Emsp=kruskal (side[i]);
                if (Emsp!=ans) {num++;
            Sum+=arr[side[i]].val;
        } flag[side[i]]=1;
    } cout<<num<< "" <<sum<<endl;
} 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.