Algorithm review (1)--and look up the set

Source: Internet
Author: User

  Turn over the 286 questions I have done, I found that I forgot a lot of things I do not remember what ah ...

 so decided that the teacher let a good review review.

  

  The first section---and check the set

1.what is and check set??

  And check set, in some of the set of n elements to apply the problem, we usually start with each element to form a set of elements, and then in a certain order to the same group of elements belonging to the collection, in the meantime to repeatedly find an element in which collection. This kind of problem repeatedly appeared in the international and domestic topic of information science in recent years, its characteristic is seemingly not complex, but the data quantity is very big, if uses the normal structure to describe, often in the space is too large, the computer cannot bear, even if in the space reluctantly passes, the operation time complexity is extremely high, It is impossible to calculate the required results of the test in the run time (1-3 seconds) specified in the game, and can only be described by a set.

(Baidu Encyclopedia ... A bunch of crap)

  My opinion:

As the name implies, and check the set, (and/check set) This algorithm is to merge the collection of lookups.

(Images from CYJB's blog)

  2. How Nong??

Two operations: 1. Check;

2. and.

Below, we will introduce the check and the separately.

We can use a tree to represent a collection, and the root node can represent the collection.

Find: In, for example, find the element D, then look up, find the root node, it also determines the D point where the collection.

Merge: Merging the two collection s1,s2, you can connect the root node of S1 (or S2) to another tree as a child of another tree.

  3. Basic code implementation

  Find: Find the root node of X

int Find (int  x) {    if(fa[x]==x)
return x; int t=find (fa[x]); return t;}

  Merge:

void Merge (int x,int  y) {    x=find (x);//Find root node    y= find(y);     if (x==y)
return ; Fa[x]=y;//points The father of X (each father) to Y;}

  4. Optimization

  We find such a point to find, one by one point, is really too slow.

The result is two optimizations.

1. Heuristic Merging

A tree with fewer nodes is connected under a tree with many nodes.

Why?? Good understanding, and then check, walk the node is less.

Code

    

void morge (int x,int  y) {    int fx=find (x);     int fy=find (y);     if (s[fx]>s[fy])    {        F[fy]=FX;        S[FX]+ =S[fy]    ;    } Else     {        f[fx]=fy;        S[FY]+ =s[fx];}    } // the number of s array is stored

2. Path compression

The parent node of all nodes on this path is set to the root node when it is found, which can greatly reduce the number of lookups later.

int Find (int  p) {    if(f[p]!=p)        f[p]=find (f[p]);     return F[p];
}

Here's an example.

Topic Source https://neooj.com:8082/oldoj/problem.php?id=1046

1046: Relatives time limit: 1 Sec memory limit: up to MB
Submitted by: 480 resolution: 251
Submitted State [Discussion Version] Title Description

Perhaps you do not know that one of your friends is your relative. He may be your great-grandfather's grandfather son-in-law's niece's cousin's grandson. If it is possible to get a complete pedigree, it would be feasible to judge whether or not two relatives should be, but if the recent public ancestor of two has been separated from them for several generations, making the genealogy very large, then it is impossible to examine relatives. In this case, the best helper is the computer. In order to simplify the problem, you will get some information about kinship, such as marry and Tom are relatives, Tom and Ben are relatives, and so on. From this information, you can launch marry and Ben are relatives. Please write a program that gives the quickest answer to our questions about kinship.

Input

The input is made up of two parts. The first part begins with N,m. N for the number of people involved in the problem, m indicates already know m to relatives close 1<=n,m<=100000, next m line, each line has two number AI, Bi, indicates known AI and bi are relatives. These people are numbered,..., N. Next Enter an integer p (1<=p<=100000), which indicates that there is a P-query, followed by P-line, each behavior ci, Di, which indicates whether CI and di are relatives.

Output

For each query output line: If Ci and di are relatives, then output "Yes", otherwise output "No".

Sample inputTen 72 45 71 38 91 25 62 333 47 Ten8 9Sample outputYesNoYesTitle Analysis: This is titled and check the Naked topic. Each person is considered a collection, if a is a relative of B, a A, a, will be merged. look for ancestors, ancestors are a, yes, otherwise no.
#include <stdio.h>intf[100001],s[100001];intFindintp) {    if(f[p]!=p) {F[p]=find (f[p]); }        returnf[p];}voidMorge (intXinty) {    intfx=find (x); intfy=find (y); if(s[fx]>S[fy]) {F[fy]=FX; S[FX]+=S[fy]; }    Else{F[FX]=fy; S[FY]+=S[FX]; }}intMain () {intN, M, a, B, X, y; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {F[i]=i; S[i]=1; }     for(intI=1; i<=m;i++) {scanf ("%d%d",&x,&y);    Morge (x, y); }    intp; scanf ("%d",&p);  for(intI=1; i<=p;i++) {scanf ("%d%d",&a,&b); if(Find (a) = =find (b)) {printf ("yes\n"); }        Else{printf ("no\n"); }    }    }

Konjac Konjac's first blog post, if you don't understand the place, or the mistake, please correct it, put it in the comments.

Algorithm review (1)--and look up the set

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.