BZOJ3624 [Apio2008] Free Road

Source: Internet
Author: User

Is the idea of greed ... Take 1 and then fetch 0 of the edges.

Once you have verified that you are not satisfied with the requirements, do the spanning tree again.

Anyway Kruskal is O (m) without pressure 2333

1 /**************************************************************2 problem:36243 User:rausen4 language:c++5 result:accepted6 time:60 Ms7 memory:16416 KB8 ****************************************************************/9  Ten#include <cstdio> One   A using namespacestd; - Const intN =20005; - Const intM =100005; the Const intMaxLen =15000010; -   - structEdges { -     intx, y; + edges () {} -Edgesint_x,int_y): X (_x), Y (_y) {} + }e[m]; A   at intN, M, K, CNT; - intFa[n]; - BOOLVis[m]; -   - CharBuf[maxlen]; - intLen, left; in   -InlineintRead () { to     intx =0; +      while(Buf[left] <'0'||'9'< Buf[left]) + +Left ; -      while('0'<= Buf[left] && Buf[left] <='9') thex = x *Ten+ buf[left++]-'0'; *     returnx; $ }Panax Notoginseng     - intLen =0, pr[ the]; theInlinevoidPrintintx) { +     if(X <0) Putchar ('-'), x =-x; A      while(x) thePr[++len] = x%Ten, x/=Ten; +     if(!len) Putchar ('0'); -      while(len) $Putchar (pr[len--] +'0'); $ } -   -   the intFIND_FA (intx) { -     returnFA[X] = = x? X:FA[X] =Find_fa (fa[x]);Wuyi } the   - intMain () { Wu     intI, X, Y, F, L, R, FA1, FA2; -Len = Fread (buf,1, MaxLen, stdin); AboutBuf[len] =' '; $n = Read (), M = Read (), k =read (); -      for(i =1, L =0, R = m +1; I <= m; ++i) { -x = Read (), y = Read (), F =read (); -         if(f) E[++l] =edges (x, y); A         ElseE[--R] =edges (x, y); +     } the       -      for(i =1; I <= N; ++i) Fa[i] =i; $      for(i =1; I <= m; ++i) the         if((FA1 = Find_fa (e[i].x))! = (FA2 =Find_fa (E[I].Y))) { theFA[FA1] =FA2; the             if(I >L) theVis[i] =1, cnt + =1; -         } in     if(CNT >k) { thePuts"No solution"); the         return 0; About     } the       the      for(i =1; I <= N; ++i) Fa[i] =i; the      for(i = R; I <= m; + +)i) +         if(Vis[i]) Fa[find_fa (e[i].x)] =Find_fa (E[I].Y); -      for(i = R; I <= m; + +)i) the         if(!vis[i] && cnt < K && (fa1 = Find_fa (e[i].x))! = (FA2 =Find_fa (E[I].Y))) {BayiFA[FA1] = FA2, vis[i] =1; the++CNT; the         } -     if(CNT! =k) { -Puts"No solution"); the         return 0; the     } the       the      for(i =1; I <= L; ++i) -         if((FA1 = Find_fa (e[i].x))! = (FA2 =Find_fa (E[I].Y))) theFA[FA1] = FA2, vis[i] =1; the      for(i =1; I <= m; ++i) the         if(Vis[i]) {94Print (e[i].x), Putchar (' '); thePrint (E[I].Y), Putchar (' '); thePrint (i <= L), Putchar ('\ n'); the         }98}
View Code

(P.S. With IO plug cheated a rank.1, liquid! )

BZOJ3624 [Apio2008] Free Road

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.