Title Link: http://www.lydsy.com/JudgeOnline/problem.php?id=3624
Test instructions
Give you an no-show graph, n-dots, M-bar edges.
There are two kinds of sides, the species are represented by 0 and 1 respectively.
Let you ask for a spanning tree so that there are just 0 kinds of edges in this tree. Outputs the end points and kinds of each edge.
If there is no solution, the output "no solution".
Exercises
Let 0 and 1 respectively make two sides of the edge right.
Steps:
(1) Find the 0 sides that must be selected first. (Priority 1, maximum spanning tree)
(2) Add the number of 0 sides to K.
(3) Fill up 1 sides.
AC Code:
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <algorithm>5#include <vector>6 #defineMax_n 200057 8 using namespacestd;9 Ten structEdge One { A intSour; - intdest; - intLen; theEdge (int_sour,int_dest,int_len) - { -Sour=_sour; -dest=_dest; +len=_len; - } + Edge () {} AFriendBOOL operator< (ConstEdge &a,ConstEdge &b) at { - returna.len<B.len; - } - }; - - intn,m,k; in intPar[max_n]; - BOOLFailed=false; toVector<edge>Edge; +Vector<edge>est; -Vector<edge>ans; the * voidRead () $ {Panax NotoginsengCin>>n>>m>>K; - inta,b,c; the for(intI=0; i<m;i++) + { ACin>>a>>b>>C; the Edge.push_back (Edge (A,b,c)); + } - } $ $ voidInit_union_find () - { - for(intI=1; i<=n;i++) the { -par[i]=i;Wuyi } the } - Wu intFindintx) - { About returnpar[x]==x?x:par[x]=find (Par[x]); $ } - - voidUniteintXinty) - { A intpx=find (x); + intpy=find (y); the if(px==py)return; -par[px]=py; $ } the the BOOLSame (intXinty) the { the returnFind (x) = =find (y); - } in the voidMax_tree () the { About Init_union_find (); the sort (Edge.begin (), Edge.end ()); the intCnt=0; the for(intI=edge.size ()-1; i>=0; i--) + { -Edge temp=Edge[i]; the if(!Same (temp.sour,temp.dest))Bayi { thecnt++; the Unite (temp.sour,temp.dest); - if(temp.len==0) Est.push_back (temp); - } the } the if(cnt!=n-1|| Est.size () >k) failed=true; the } the - voidMin_tree () the { the Init_union_find (); the intCnt=0;94 intSpe=0; the for(intI=0; I<est.size () && spe<k;i++) the { theEdge temp=Est[i];98cnt++; Aboutspe++; - Ans.push_back (temp);101 Unite (temp.sour,temp.dest);102 }103 for(intI=0; I<edge.size (); i++)104 { theEdge temp=Edge[i];106 if(!Same (temp.sour,temp.dest))107 {108 if(temp.len==0)109 { the if(spe<k)111 { thecnt++;113spe++; the Ans.push_back (temp); the Unite (temp.sour,temp.dest); the }117 }118 Else119 { -cnt++;121 Ans.push_back (temp);122 Unite (temp.sour,temp.dest);123 }124 } the }126 if(cnt!=n-1|| SPE!=K) failed=true;127 } - 129 voidSolve () the {131 Max_tree (); the Min_tree ();133 }134 135 voidprint ()136 {137 if(failed)138 {139cout<<"No solution"<<Endl; $ return;141 }142 for(intI=0; I<ans.size (); i++)143 {144Edge temp=Ans[i];145cout<<temp.sour<<" "<<temp.dest<<" "<<temp.len<<Endl;146 }147 }148 149 intMain () Max {151 read (); the solve ();153 print ();154}
Bzoj 3624 [Apio2008] free road: And check set + spanning Tree + greedy "just have K-Bar special path"