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>>ARR[I].A>>ARR[I].B>&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; }