HDU 1232 smooth Engineering

Source: Internet
Author: User

This topic is also a typical use of the Minimum Spanning Tree Algorithm. Different from other questions, the other requirement is that the minimum number of edges to be added, so that any two
Points are related. The query set algorithm is used to calculate the number of times the two paths are merged Based on the map given by the question, even if the minimum number of paths to be added is used.

1 # include <stdio. h>
2 # include <stdlib. h>
3
4 int father [1001], tot; // father [I] records the I BOSS!
5 // tot count the minimum number of paths to be added at first!
6
7 int find (int x)
8 {// find the BOSS of x!
9 int r = x;
10 while (r! = Father [r]) r = father [r];
11 return r ;//
12}
13
14 void join (int a, int B)
15 {// unify the BOSS of a and B!
16 int fa = find (a), fb = find (B );
17 if (fa! = Fb)
18 {
19 father [fa] = fb;
20 tot --; // unifies the BOSS of the two camps at a time, so the number of paths to be added is reduced by one!
21}
22}
23
24 int main ()
25 {
26 int n, m, x, y;
27 while (scanf ("% d", & n), n)
28 {
29 scanf ("% d", & m );
30 tot = n-1; // The number of minimum edges required by the initialization tot equals to n points!
31 father [n + 1];
32 for (int I = 1; I <= n; I ++) father [I] = I; // initialize your own BOSS!
33
34 for (int I = 1; I <= m; I ++)
35 {
36 scanf ("% d", & x, & y );
37 join (x, y );
38}
39 printf ("% d \ n", tot); // outputs the number of edges required on an existing basis!
40}
41 return 0;
42}
43

Related Article

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.