HDU---1232---Unblocked project (combined investigation)

Title description:

A certain province surveyed urban traffic conditions and obtained a statistical table of existing urban roads. The table lists the cities and towns directly connected to each road. The goal of the Provincial Government's "Unblocked Project" is to enable traffic between any two towns in the province (but not necessarily directly connected by roads, as long as each other can be reached indirectly through roads). Ask at least how many roads need to be built?

enter:

The test input contains several test cases. The first line of each test case gives two positive integers, which are the number of towns N (<1000) and the number of roads M; the subsequent M lines correspond to M roads, and each line gives a pair of positive integers, respectively The number of two towns directly connected by a road. For simplicity, the towns are numbered from 1 to N.
Note: There can be multiple roads between the two cities, which means that the input of
3 3
1 2
1 2
2 1
is also legal.
When N is 0, the input ends and the use case is not processed.

Output:

For each test case, output the minimum number of roads that need to be built in one line.

Example:

Input:
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
Output:
1
0
2
998


Ideas:

This question first gives out which roads are connected, that is, connected roads can be regarded as a set, and all roads can be connected, that is, there is only one set in the end, because the output result is: the number of sets -1 , That is, the number of roads that need to be built. Obviously, it can be done by using combined search. Let’s talk about combined search .


And check set:

As the name implies, there are two operations: merge and search

  • Merge: merge two collections
  • Find: Find which collection an element belongs to

Here directly talk about the most efficient implementation method: tree

  • Each set is represented by a tree root
    • Define the array Set[1, …, n]:
    • Set[i] = i, then i represents the set, and is the root of the tree corresponding to the set
    • Set[i] = j, if j ≠ i, then j is the parent node of i

Insert picture description here


Let's talk about his merge and search operations:

merge

Combine set a with set b, the following operation is to merge set a into set b

void merge(int a,int b)
{
    
    
	Set[a]=b;
}

Find

Find who is the root (boss) of node x, if it is the root, Set[x]==x; just return directly, if not, assign r to his parent node, and then keep looking for the root node .

int findx(int x)
{
    
    
	int r=x;
	while(Set[r]!=r)
		r=Set[r];
	return r;
}

After knowing and checking the collection, this problem will be solved easily, look at the code below:

#include <iostream>
using namespace std;
int bin[1005];

int findx(int x)
{
    
    
	int r=x;
	while(bin[r]!=r)
		r=bin[r];
	return r;
}

void merge(int x,int y)
{
    
    
	int fx=findx(x);
	int fy=findx(y);
	if(fx!=fy)
		bin[fx]=fy;
}

int main()
{
    
    
	int n,m,x,y,count;
	while(cin>>n&&n)
	{
    
    
		count=0; 
		for(int i=1;i<=n;i++)
			bin[i]=i;
		cin>>m;
		for(int i=1;i<=m;i++)
		{
    
    
			cin>>x>>y;
			merge(x,y);
		}	
		
		for(int i=1;i<=n;i++)
		{
    
    
			if(bin[i]==i)
				count++;
		}
		cout<<count-1<<endl;
	}		
	return 0;
} 


Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113888704