Sample Input5 7 2
1 3 0
4 5 1
3 2 0
5 3 1
4 3 0
1 2 1
4 2 1
Sample Output3 2 0
4 3 0
5 3 1
1 2 1
this problem at that time read the question for half a day, now probably translated:we need to build a spanning tree for the graph so that all points are connected, and this tree has only k white edges. read to think of the [National training Team 2012]tree (Chen Lijie), the problem is similar, the number of white edges is exactly the need, but the problem requires the minimum spanning tree, and this problem as long as the tree is the only way to build. And it has to be judged and illegal. Then began to think, suppose we first pull off all the white edge, then the rest is a black edge made up of a connecting block. And then what? Do not know, at first want to go to use and check the set, but did not make anything, also give up. The positive solution is still the smallest spanning tree, the sum, perhaps should not be said to be the smallest spanning tree, but really want to use Kruskal, we first black edge priority, so that we can first find out the lower bound of a spanning tree, if K is smaller than he is obviously not. In the same way, we will take precedence over the white side and find the upper limit of the spanning tree, if K is bigger than he is still not. legitimacy we have finished, how to come out of the answer? let us first look back at one of the properties of the tree: When we are on a tree, the tree is destroyed when we connect from one point to another, but the tree becomes legitimate when we take apart any point on the ring that consists of these two sides. first of all, we find the white edge of the white edge to find out when we can not find the black side to replace them, plainly, we must choose them. second, for the rest of the white edges, we can realize that the reason why we did not put them in a limited time to find it is because it can be a black or white edge instead, if it is replaced by the white side, then we still do not have to choose him, because the white edge is to be chosen, of course, we can take it and the white edge But since it's SPJ, what's the point? if he is replaced by the black side, then we do not have to choose the black edge, so we must choose the white edge, and then greedy to find those who are black side replaced by the white side, find enough to only find the black side, the final output of the answer is good.
1#include <iostream>2#include <cstdlib>3#include <cstdio>4#include <cstring>5#include <queue>6#include <algorithm>7#include <cmath>8#include <map>9 #defineN 20005Ten #defineM 100005 One using namespacestd; A intN,m,t,fa[n]; - structro - { the intTo, from, L; - BOOLBJ; - }road[m]; - BOOLpx1 (ro a,ro b) + { - returnA.l>B.L; + } A BOOLpx2 (ro a,ro b) at { - returna.l<B.L; - } - intFindintx) - { - if(fa[x]==x)returnx; in returnfa[x]=find (Fa[x]); - } to voidHbintXinty) + { - intA=find (x), b=find (y); thefa[a]=b; * } $ intAns[n];Panax Notoginseng intMain () - { thescanf"%d%d%d",&n,&m,&t); + intsum=0; A for(intI=1; i<=n;i++) fa[i]=i; the for(intI=1; i<=m;i++) + { -scanf"%d%d%d", &road[i]. from,&road[i].to,&road[i].l); $ if(!ROAD[I].L) sum++; $ } -Sort (road+1, road+1+m,px1); - intjs1=0, js2=0; the for(intI=1; i<=m;i++) - {Wuyi intX=road[i]. from, y=road[i].to; the if(Find (x)! =find (y)) - { Wujs1++; - if(!road[i].l) About { $road[i].bj=1; -js2++; - } - HB (x, y); A } + if(js1==n-1) Break; the } - if(js1!=n-1|| Js2>t) $ { theprintf"No solution\n"); theExit0); the } theSort (road+1, road+1+m,px2); -js1=0, js2=0; in for(intI=1; i<=n;i++) fa[i]=i; the for(intI=1; i<=sum;i++) the { About if(ROAD[I].BJ) the { the intX=road[i]. from, y=road[i].to; the HB (x, y); +js2++; -js1++; theans[js1]=i;Bayi } the } the for(intI=1; i<=m;i++) - { - intX=road[i]. from, y=road[i].to; the if(Find (x)! =find (y)) the { the if(!road[i].l) the { -js2++; the } thejs1++; theans[js1]=i;94 HB (x, y); the } the if(js2==t) i=sum,js2=-1; the }98 if(js2!=-1) About { -printf"No solution\n");101Exit0);102 }103 for(intI=1; i<=n-1; i++)104 { theprintf"%d%d%d\n", Road[ans[i]]. from, ROAD[ANS[I]].TO,ROAD[ANS[I]].L);106 }107 return 0;108}
View Code
Bzoj 3624: [Apio2008] Free road (greedy + spanning tree)