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