Test instructions
One figure 0, 12 sides, constructs a spanning tree with a K-bar 0-side
Prioritize 1-side construction spanning tree to see if 0 sides are less than K
Then keep these 0 sides , complement the K bar, plus 1 sides must be able to form a spanning tree
Proof of similar Kruskal
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn=2e4+5, m=1e5+5; typedefLong LongLl;inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intN, M, K, u, V, C, M0, M1, p;structmeow{intu, V, c;} A[M], b[m], ans[n];intFa[n];intFindintx) {returnX==FA[X]? x:fa[x]=find (Fa[x]);}intFlag[n];intMain () {Freopen ("inch","R", stdin); N=read (); M=read (); k=read (); for(intI=1; i<=m; i++) {u=read (), V=read (), c=read (); if(c==1) a[++m1]=(meow) {u,v,c}; Elseb[++m0]=(meow) {u,v,c}; } intCnt=0; for(intI=1; i<=n; i++) fa[i]=i; for(intI=1; i<=m1; i++) {u=A[I].U, v=a[i].v; intX=find (u), y=Find (v); if(x==y)Continue; FA[X]=y; if(++cnt = = N1) Break; } for(intI=1; i<=m0; i++) {u=B[I].U, v=b[i].v; intX=find (u), y=Find (v); if(x==y)Continue; FA[X]=y; Ans[++p]=b[i]; flag[i]=1; if(++cnt = = N1) Break; } if(P > K | | cnt < n1) {puts ("No solution");return 0;} for(intI=1; i<=n; i++) fa[i]=i; for(intI=1; i<=p; i++) Fa[find (ans[i].u)] =find (ANS[I].V); CNT=p; if(cnt<k) for(intI=1; i<=m0; i++)if(!Flag[i]) {u=B[I].U, v=b[i].v; intX=find (u), y=Find (v); if(x==y)Continue; FA[X]=y; ans[++cnt]=B[i]; if(cnt = = k) Break; } for(intI=1; i<=m1; i++) {u=A[I].U, v=a[i].v; intX=find (u), y=Find (v); if(x==y)Continue; FA[X]=y; ans[++cnt]=A[i]; if(cnt = = N1) Break; } for(intI=1; i<=cnt; i++) printf ("%d%d%d\n", Ans[i].u, ANS[I].V, ans[i].c);}
Bzoj 3624: [Apio2008] Free road [spanning tree and collection]