And query the set template-implements the path pressure

Source: Internet
Author: User

Today, I spent some time learning and querying datasets online. First, the example is parsed in the comment.

Question: Relations)

Maybe you don't know. One of your friends is your relative. He may be the grandson of your great-grandfather's son-in-law's nephew. If a complete family tree can be obtained, it is feasible to determine whether two people are relatives or not. However, if the recent common ancestor of two people is several generations separated from them, the family tree is very huge, then we can test the relationship between relatives. in this case, the best helper is the computer. </

To simplify the problem, you will get information about the kinship, such as marry and Tom, and Tom and B en. From this information, you can introduce that marry and Ben are relatives. Please write a program to give answers to questions about our relatives as quickly as possible.

The input in reference input/output format is composed of two parts.

The first part starts with N, M. N is the number of persons involved in the problem (1 ≤ n ≤ 20000 ). The numbers of these persons are 1, 2, 3 ,..., N. Below are m rows (1 ≤ m ≤ 1000000), each row has two numbers of AI, Bi, indicating that known AI and Bi are relatives.

The second part starts with Q. The following Q rows contain Q queries (1 ≤ q ≤ 1 000 000). Each behavior CI, Di, indicates asking whether Ci and Di are relatives.

For each query CI, Di, If Ci and Di are relatives, yes is output; otherwise, no is output.

Sample Input and Output

Enter relation. In

10 7

2 4

5 7

1 3

8 9

1 2

5 6

2 3

3

3 4

7 10

8 9

Output relation. Out

Yes

No

Yes

# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; # define maxn 100int father [maxn], rank [maxn]; // father indicates the father's serial number of the node, and rank indicates the generation of the node ". For example, in 1-> 2-> 3-> 4, the generation of 4 is 3.int N; void makeset () {int I; for (I = 1; I <= N; I ++) {FATHER [I] = I; // because there is no father, mark the father as oneself, and some can also be marked as-1. rank [I] = 0; // no children, so the generation score is 0 .}} int getfather (INT v) // find the grandfather of the node, that is, the top node. {If (father [v] = V) return V; else {FATHER [v] = getfather (father [v]);/* implements path compression: for example: if the relationship between the original nodes is 1-> 2-> 3-> 4, the grandfathers of 1, 2, and 4 are 4. however, in this Recursive Backtracking process, Father [v] = getfather (father [v]); points the father values of 1, 2, 3, and 4 to 4, it is changed to 1-> 4, 2-> 4, 3-> 4-> 4. when searching for the grandfather of 1, you only need to find it once, instead of the original three times. This structure makes the search faster and the path compression is quick. */Return father [v] ;}} void Union (int x, int y) // If x and y do not belong to a set, merge them into a set, the operation is to turn X's grandfather into Y's grandfather. {// Note that X's grandfather is changed to Y's grandfather by default. If y's grandfather's rank is larger than X's grandfather's rank, then, let y's become X's grandfather. // Void judge (int x, int y) will consider this situation. Int FX, FY; FX = getfather (x); FY = getfather (y); If (FY! = FX) Father [FX] = FY;} bool same (int x, int y) // determines whether it is the same set {return getfather (X) = getfather (y);} void judge (int x, int y) // in void Union (int x, int y), the grandfather of X is changed to Y by default, if the rank of Y is larger than the rank of X, then the father of Y is changed to X. In this way, the path is compressed. {// If (rank [FX]> rank [FY]), the generation of Y + 1.int FX, FY; FX = getfather (x); FY = getfather (y ); if (rank [FX]> rank [FY]) Father [FY] = FX; else {FATHER [FX] = FY; if (rank [FX] = rank [FY]) + + rank [FY] ;}} int main () {int m, I; int A, B; while (CIN> N> m) {makeset (); for (I = 1; I <= m; I ++) {CIN> A> B; if (! Same (a, B) // judge whether it is a set {Union (a, B); Judge (A, B ); // implement path compression} CIN> m; for (I = 1; I <= m; I ++) {CIN> A> B; if (same (a, B) cout <"yes" <Endl; elsecout <"no" <Endl ;}} 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.