bzoj3624: [Apio2008] Free Road

Source: Internet
Author: User

Specific title: https://vjudge.net/problem/HYSBZ-3624

Description

A kingdom has n cities and M-strips of road, some of which are cobblestone roads and some are cement road. Now the king has to choose as few roads as possible for free, and make every two cities a free route. The king intends to keep just the K-Cobblestone road. Will the king be able to do all these requirements if it can output the path of the two cities and the type of road, otherwise "no solution".

HINT
N <= 20000
M <= 100000

Analysis
If it succeeds, it must be the smallest spanning tree.
The practice is to deform the Kruskal.
Connect all the cement road and then join the cobblestone Road to get the cobblestone paths that must be used (if more than K Direct no solution), and then press Kruskal to connect the cobblestone path to the K bar (if not to direct no solution), and then continue to follow the Kruskal connection Cement Road.

Spit Groove: At the end of the text must be changed (WA has nearly 20 rounds).

Code

1#include <iostream>2#include <cstdio>3#include <cctype>4 using namespacestd;5 Const intMAXN =20020;6 Const intMAXM =100010;7 intN, M, K, Fat[maxn], cnt0, cnt1, num;8 BOOLCHS[MAXM];9 structEdgeTen { One     intu, V, c; A } E[MAXM]; -  -InlineintReadint () the { -     Charc =GetChar (); -      while(!isdigit (c)) C =GetChar (); -     intx =0; +      while(IsDigit (c)) -     { +x = x *Ten+ C-'0'; Ac =GetChar (); at     } -     returnx; - } -  - intUnionset (intu) - { in     returnFat[u] = = u? U:fat[u] =Unionset (Fat[u]); - } to  + intMain () - { then =readint (); *m =readint (); $K =readint ();Panax Notoginseng      for(inti =1; I <= N; ++i) Fat[i] =i; -      for(inti =1; I <= m; ++i) the     { +E[I].U =readint (); AE[I].V =readint (); theE[I].C =readint (); +     } -      for(inti =1; I <= m; ++i) $     { $         if(E[I].C = =1) -         { -             intR1 =Unionset (e[i].u); the             intr2 =Unionset (E[I].V); -             if(R1! =R2)Wuyi             { theFAT[R1] =R2; -++cnt1; Wu             } -         } About     } $      for(inti =1; I <= m; ++i) -     { -         if(E[I].C = =0) -         { A             intR1 =Unionset (e[i].u); +             intr2 =Unionset (E[I].V); the             if(R1! =R2) -             { $Chs[i] =true; theFAT[R1] =R2; the++cnt0; the             } the         } -     } in     if(cnt0 + Cnt1! = N-1|| Cnt0 >k) the     { theprintf"No solution\n"); About         return 0; the     } the      for(inti =1; I <= N; ++i) Fat[i] =i; thecnt0 = Cnt1 =0; +      for(inti =1; I <= m; ++i) -     { the         if(Chs[i])Bayi         { the             intR1 =Unionset (e[i].u); the             intr2 =Unionset (E[I].V); -             if(R1! =R2) -             { theFAT[R1] =R2; the++cnt0; the             } the         } -     } the      for(inti =1; I <= m; ++i) the     { the         if(E[I].C = =0&& Cnt0 <k)94         { the             intR1 =Unionset (e[i].u); the             intr2 =Unionset (E[I].V); the             if(R1! =R2)98             { AboutChs[i] =true; -FAT[R1] =R2;101++cnt0;102             }103         }104     } the     if(Cnt0! =k)106     {107printf"No solution\n");108         return 0;109     } the      for(inti =1; I <= m; ++i)111     { the         if(E[I].C = =1)113         { the             intR1 =Unionset (e[i].u); the             intr2 =Unionset (E[I].V); the             if(R1! =R2)117             {118Chs[i] =true;119FAT[R1] =R2; -             }121         }122     }123      for(inti =1; I <= m; ++i)124         if(Chs[i]) theprintf"%d%d%d\n", E[i].u, E[I].V, e[i].c);126     return 0;127}

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.