题意:
给出一个无向图,询问怎么修改边权使得这张图的最小生成树权值不变的同时只有一颗。输出最少修改次数
#include<bits/stdc++.h> using namespace std; const int maxn=2e5+100; int n,m; struct node { int u,v,w; bool operator <(const node &r) const { return w<r.w; } }edge[maxn]; int father[maxn]; int findfather (int x) { int a=x; while (x!=father[x]) x=father[x]; while (a!=father[a]) { int z=a; a=father[a]; father[z]=x; } return x; } int main () { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) father[i]=i; for (int i=1;i<=m;i++) { scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); } sort(edge+1,edge+m+1); vector<pair<int,int> > v; int ans=0; for (int i=1;i<=m;i++) { int j; v.clear(); for (j=i;j<=m;j++) { if (edge[i].w!=edge[j].w) break; int fu=findfather(edge[j].u); int fv=findfather(edge[j].v); if (fu==fv) continue; v.push_back(make_pair(fu,fv)); } for (pair<int,int> it:v) { int fa=findfather(it.first); int fb=findfather(it.second); if (fa==fb) ans++; else father[fa]=fb; } i=j-1; } printf("%d ",ans); }