3624: [Apio2008] Free Road
https://www.lydsy.com/JudgeOnline/problem.php?id=3624
Test instructions
An untyped graph with two types of 0 and 1 for each edge. Ask for a minimum spanning tree so that there are K bars 0 sides.
Analysis:
In order to meet the K-bar 0-side limit, first consider the 0-side which must be selected, if all 1 sides are joined, there are 0 sides can make the diagram is not connected, then these 0 sides have to be selected.
Add the required selection and then add to K, then add 1 sides. The middle of the sentence whether the required is greater than the k,0 edge is greater than or equal to K.
Code:
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 5InlineintRead () {6 intx=0, f=1;CharCh=getchar (); for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') f=-1;7 for(; isdigit (ch); Ch=getchar ()) x=x*Ten+ch-'0';returnx*F;8 }9 Ten Const intN =20010; One Const intM =100010; A - structedge{ - intu,v; the Edge () {} -Edge (intAintb) {u = A, V =b;} - }e[m]; - intFa[n], q[m]; + BOOLVis[m]; - + intFindintx) { A returnx = = Fa[x]? X:FA[X] =find (Fa[x]); at } - - intMain () { - - intn = Read (), M = Read (), k =read (); - in intB =0, W = m +1; - for(intI=1; i<=m; ++i) { to intU = Read (), V = Read (), C =read (); + if(c = =1) E[++b] =Edge (u,v); - ElseE[--W] =Edge (u,v); the } * $ for(intI=1; i<=n; ++i) Fa[i] =i;Panax Notoginseng - inttot =0; the for(intI=1; i<=m; ++i) { + intU = Find (e[i].u), V =find (E[I].V); A if(U = = v)Continue; theFa[u] =v; + if(i > B) vis[i] =true, Q[++tot] =i; - } $ $ if(Tot >k) { -Puts"No solution"); - return 0; the } - Wuyi for(intI=1; i<=n; ++i) Fa[i] =i; the - for(intI=1; i<=tot; ++i) WuFa[find (e[q[i]].u)] =find (E[Q[I]].V); - About for(intI=w; i<=m; ++i) { $ if(tot = = k) Break; - intU = Find (e[i].u), V =find (E[I].V); - if(U = = v)Continue; -Fa[u] =v; ATot + +; +Vis[i] =true; the } - $ if(Tot <k) { thePuts"No solution"); the return 0; the } the - for(intI=1; i<=b; ++i) { in intU = Find (e[i].u), V =find (E[I].V); the if(U = = v)Continue; theFa[u] =v; AboutVis[i] =true; the } the the for(intI=1; i<=m; ++i) + if(Vis[i]) printf ("%d%d%d\n", E[i].u, E[I].V, i<=b?1:0); - the return 0;Bayi}
3624: [Apio2008] Free Road