HDU1232 Smooth Traffic Project

topic:

Problem Description

A provincial survey of urban traffic, urban roads to get existing tables, the table lists the cities and towns each road directly connected. Target provincial government "Smooth Traffic Project" is to make the province between any two towns can implement traffic (but not necessarily directly connected to the road, as long as you can reach each other indirectly through road). Minimum asked how many roads also need to build?
 

 

Input

Test input contains several test cases. Each test case is given row of the first two positive integers, are the number of towns N (<1000) and road number M; M rows corresponding to the next M path, each row is given a pair of positive integers, respectively, the number two towns in direct communication path. For simplicity, the town numbered from 1 to N.
Note: number of roads may be communicated between the two cities, ie
. 3. 3
. 1 2
. 1 2
2. 1
This input is valid
when N is 0, an input end, the use cases are not processed.
 

 

Output

For each test case, the number of roads in at least one line in the output needed construction.
 

 

Sample Input

4 2 1 3 4 3 3 3 1 2 1 3 2 3 5 2 1 2 3 5 999 0 0
 

 

Sample Output

1 0 2 998
Hint
Hint
Huge input, scanf is recommended.

Ideas:

  • Apply disjoint-set template

Code:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  
. 4  int pre [ 1005 ]; // store predecessor node
 . 5  
. 6  void the init ( int n) // initialize the n nodes
 . 7  {
 . 8      for ( int I = . 1 ; I <= n-; I ++ )
 . 9          pre [I] = I; // make root node of each node for themselves, i.e., each is a separate nodes
 10  }
 . 11  
12 is  int find ( int X) // find the current the root node
 13 is  {
 14      the while (X! = pre [X])
 15         = X pre [X];
 16      
. 17      return X;
 18 is  }
 . 19  
20 is  void Judge ( int FX, int FY) // two nodes is determined as having a root node
 21 is  {
 22 is      IF (FX =! FY) // If the root node of the two nodes included in different combined
 23 is          pre [FX] = FY;
 24  }
 25  
26 is  int main ()
 27  {
 28      int n-, m, X, Y, FX, FY;
 29      the while (Scanf ( " % D " , & n-)! && the EOF = n-)
 30      {
 31 is         Scanf ( " % D " , & m);
 32          int CNT = n-- . 1 ; // n-th node to the n-1 communication has to be repaired passage
 33 is          the init (n-);
 34 is          for ( int I = 0 ; I <m; I ++ )
 35          {
 36              Scanf ( " % D% D " , & X, & Y);
 37 [              FX = Find (X);
 38 is              FY = Find (Y);
 39              Judge (FX, FY);
 40          }
 41 is          for ( int I =. 1 ; I <= n-; I ++ )
 42 is          {
 43 is              IF ! (Pre [I] = I) // if the current node is not the root node has been repaired in this passage
 44 is                  cnt-- ;
 45          }
 46 is          the printf ( " % D \ n- " , CNT);
 47      }
 48      return  0 ;
 49 }

 

Guess you like

Origin www.cnblogs.com/Anber82/p/11140887.html