Get started with a check set

Source: Internet
Author: User

I was looking at "Aha!" Algorithm "This book completed and find the introduction of the set, I would like to cite another chestnut, but ... The chestnut in the book is very apt.

First introduce a problem: known to have 10 bandits, the police need a little bit of trace and finally dug up their respective behind the gang Yiguoduan, after a period of time the investigation of the police got 9 exact clues, respectively, can explain the two bandits of the relationship. So how many gangs will be knocked out of this operation altogether?

Enter the following data:

Follow the first line to enter the number of people n, the number of threads m, the next m line input clues, each line of clues such as 1 2 for Bandits 1th and 2nd is a gang. A set of data is now available for follow-up and program testing:

<span style= "FONT-SIZE:18PX;" >10 (4</span>)
How many outputs should this set of data be? Let's make a little analysis.


1: First to apply for a one-dimensional array f, with subscript 1~10 to mark the 10 bandits, each element corresponding to the number of each of the bandits to represent the boss who.


2: Initialization, first we assume that each bandit "fragmented", that is, F[1]=1, f[2]=2 ... f[i] = i;


3: Start processing Clues, first clue 1 2, you can find 1th and 2nd is a gang, then in the end is to make 1th surrender 2nd or 2nd to surrender 1th? Here we first follow the "left" rule, let number 2nd, that is, a f[2] = 1; At this point the F array becomes

1 1 3 4 5 6 7 8 9 10

Then start processing the second clue, 3 4, in the same way according to the "left law" f[4] = 3; At this point the F array becomes:

1 1 3 3 5 6 7 8 9 10

Continue the third Clue 5 2, known f[5] = 5, description of the boss of 5th is himself, for 2nd f[2] = 1; Note 2nd is the boss of 1th, at this time still in accordance with the "left" rule, let 2nd surrender 5th is f[2] = 5; Then the robber of the number 1th will quit. Why the Gun my Man (f[2] =1)? When there is contradiction, how to do??? Introduce another law at this time-----> The thief first to seize the king!! Directly find 2nd boss talk, let 2nd number of Boss 1th surrender 5th is f[1]=5, conflict resolution!! The third thread is finished, at this point the F array is:

5 5 3 3 5 6 7 8 9 10

Here's the rule of the left and the catch thief. The 6 clues after the king's rule can be handled smoothly, not in the process of repeating, the F array after each processing is given directly

5 5 3 3 5 3 7 8 9 10

5 5 5 3 5 5 78 9 10

5 5 5 3 5 5 8 8 9 10

5 5 5 3 5 5 9 9 9 10

The ultimate gang relationship is clearly present:

2 1 3 4 6 boss is number 5th

Number 10th, be your own boss.

8 7 boss is number 9th.

So a total of three gangs! Problem solved satisfactorily,

PS: The detailed process can refer to the "Aha!" Algorithm "200 page, the following gives the problem solving code:


<span style= "FONT-SIZE:18PX;"   > #include <cstdio> #include <iostream>using namespace std; int f[1000];            Define individual elements int n,m;           The total number of N and the number of known relationships int x, y;    A set of relationships such as 1 2 for 1th number 2nd for a gang of void init ()//initialization {for (int i = 1; i<=n; i++) {f[i] = i;    }}int getf (int v)//Keep looking for your host {if (f[v] = = v) return v;        else {F[v] = GETF (F[v]);    return F[V];    }}void merge (int v,int u)//merge function {int t1,t2;    T1 = GETF (v);    t2 = GETF (u);    if (t1! = t2)//According to the principle of the left priority, let T2 submit T1 {f[t2] = T1;        }}int Main () {while (scanf ("%d%d", &n,&m)!=eof) {init ();            for (int i = 0; i<m; i++) {scanf ("%d%d", &x,&y);        Merge (x, y);      } int sum = 0;        Statistic gang number sum for (int i = 1; i<=n; i++) {if (f[i] = = i) sum++;    } printf ("A total of%d gang \ n", sum); } return 0;} </span>





Get started with a check set

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.