Disjoint-set -B - Smooth Traffic Project

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

  

Sample Output

 1

0

2

998 
ideas, and check collection, and find out the number of the sets of minus one is the answer.

#include <cstdio>
using namespace std;
 
const int maxn = 3e4 + 5;
int pre[maxn];
 
int find(int x)
{
    return pre[x] == x ? x : find(pre[x]);
}
 
void join(int x, int y)
{
    x = find(x);
    y = find(y);
    if(x!=y)pre[y] = x;    
}
 
int main()
{
    int n, m, ans, i, j, k, fir, get;
    while(scanf("%d%d", &n, &m),n)
    {
        ans = 0;
        for(i = 1; i <= n; i++)
        {
            pre[i] = i;
        }
        for(i = 1; i <= m; i++)
        {
           scanf("%d%d", &k, &fir);
           join(k, fir);
        }
        for(i = 1; i <= n; i++)
        {
            if(find(i) == i)
                ans++;
        }
        printf("%d\n", ans-1);
    }
    return 0;
}

 

 

template

#include <bits / STDC ++ H.> // Path compressing 
the using  namespace STD;
 #define MAX_N 10000
 int PAR [MAX_N];
 int Rank [MAX_N]; // tree height 
void the init ( int n-) // initialize 
{
     for ( int = I 0 ; I <n-; I ++ ) 
    { 
        PAR [I] = I; 
        Rank [I] = 0 ; 
    } 
 } 
 
int Find ( int X) // query tree root 
{
     return? PAR [the X-] == the X-the X-: PAR [the X-] == the Find (PAR [the X-]);
 //     int r = the X-;
 //     the while (! PAR [r] = r)
 //         r = PAR [r ];
         // query: returns the root of x
 //     int x = I, J;
 //     the while (I = R & lt!)
 //     {
 //         J = PAR [I];
 //         PAR [I ] = R & lt;
 //         I = J;
 //     }
 //    return R & lt; 
} 
 
void Unite ( int X, int Y) // were combined, x, y in the set 
{ 
    X = Find (X); 
    Y =Find (Y);
     IF (! X = Y) 
    PAR [Y] = X;
 //     IF (X == Y) return;
 //     IF (Rank [X] <Rank [Y])
 //     {
 //         PAR [X] = Y;
 //         
//     }
 //     the else
 //     {
 //         PAR [Y] = X;
 //         IF (Rank [X] == Rank [Y])
 //         Rank [X] ++;
 //         
//     } 
} 
 
BOOL same ( int x, int y) // determines x and y are on the same set 
{
     return Find (x) ==Find (Y); 
} 

int main () 
{ 
    for (I = . 1 ; I <= n-; I ++) // note from the subject of the request from a start or zero: 
    { 
            PAR [I] = I; 
    } 
//     System ( " PAUSE "); 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/lanclot-/p/11112348.html