Nyoj 925 King's troubles (Minimum Spanning Tree)

Source: Internet
Author: User
1/* 2 Question: each two cities in N cities have multiple path connections, but the number of days of the path is limited! Assume that a route does not exist 3 and N cities cannot be connected, the village names will protest! How many protests are there in total! 4 5. Train of Thought: Build the tree with the maximum edge! If there is a maximum edge, connect the node and ensure connection! If the edge weight is small, the value 6 can be ignored! At last, the de-duplication (composed of the largest edge) will be generated in the tree (the same value is only one protest )! The value of the remaining edge is 7! 8 */9 # include <iostream> 10 # include <cstring> 11 # include <cstdio> 12 # include <algorithm> 13 # include <vector> 14 # define M 1000515 # define n10000516 using namespace STD; 17 struct edge {18 int U, V, TT; 19}; 20 21 int f [N]; 22 int n, m; 23 int getfather (int x) {24 return x = f [x]? X: F [x] = getfather (F [x]); 25} 26 27 bool Union (int A, int B) {28 int fa = getfather (), fb = getfather (B); 29 If (Fa! = FB) {30 f [fa] = FB; 31 return true; 32} 33 return false; 34} 35 36 bool CMP (edge a, edge B) {37 return. tt> B. TT; 38} 39 40 edge [N]; 41 int XX [N]; 42 int main () {43 while (scanf ("% d", & N, & M )! = EOF) {44 for (INT I = 1; I <= N; ++ I) 45 f [I] = I; 46 for (INT I = 0; I <m; ++ I) 47 scanf ("% d", & edge [I]. u, & edge [I]. v, & edge [I]. TT); 48 sort (edge, edge + M, CMP); // sort edge weights in ascending order! 49 int CNT = 0; 50 for (INT I = 0; I <m; ++ I) 51 if (Union (edge [I]. u, edge [I]. v) 52 XX [CNT ++] = edge [I]. TT; 53 CNT = unique (XX, xx + CNT)-xx; // deduplicated 54 printf ("% d \ n", CNT); 55} 56 return 0; 57} 58

 

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.