HDU1232 unblocked project and introduction to the collection

Source: Internet
Author: User


Topic:

A province investigates urban traffic conditions and obtains statistical tables of existing urban roads, which list the towns where each road is directly connected. The goal of the provincial government's "unblocked project" is to enable traffic to be achieved between any two towns in the province (but not necessarily directly connected to each other, as long as they can be reached indirectly through the road). How many roads must be built at least.

Input: The test input contains several test cases. The 1th line of each test case gives two positive integers, namely the number of towns N (< 1000) and the number of roads m, followed by M-lines, each with a pair of positive integers, the number of two towns directly connected to the road. For simplicity, towns are numbered from 1 to N.
Note: There can be more than one road connection between two cities, which means
3 3
1 2
1 2
2 1
This type of input is also legal.
When n is 0 o'clock, the input ends and the use case is not processed.
Output: For each test case, the minimum number of roads required to be built in line 1.
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 tips:
Huge input, scanf is recommended.
AC Code

/* and get started HDU1232 July 28, 2017 19:41:07 */#include <stdio.h> #include <set> using namespace std;
const int maxn=1010;

int PRE[MAXN];
	int find (int x) {int r=x;
	while (Pre[r]!=r) r=pre[r];//finds the root node so far as int i=x,j;
		while (i!=r) {j=pre[i];
		Pre[i]=r;
	I=j; 
} return R;
	} void Join (int x,int y) {int fx=find (x);
	int Fy=find (y);
if (fx!=fy) pre[fx]=fy;
	} int main () {int n,m;
		while (scanf ("%d%d", &n,&m) ==2&&n) {for (int i=1;i<=n;i++) pre[i]=i;
			for (int i=1;i<=m;i++) {int A, B;
			scanf ("%d%d", &a,&b);
		Join (A, b); 
		}/* The first method, the number of ancestors, and all the nodes to traverse it again if the ancestor of a point is found to be himself, then this point must be the number of ancestors, the final result minus one, because n connected components, only the N-1 path to the N-connected components can be combined into a
		*/int ans=0;
		for (int i=1;i<=n;i++) {if (Find (i) ==i) ans++;
		} printf ("%d\n", ans-1);
		/* This is the second method, the ancestors of each point, put into the set inside, because the set to take the weight, so the number of elements inside, that is, the number of Unicom components set<int> ans;
			for (int i=1;i<=n;i++) {int r=find (i);
		Ans.insert (R);
		} printf ("%d\n", Ans.size ()-1);
*/} return 0;  }


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.