Poj 2492 A Bug's Life (query set)

Source: Internet
Author: User
Http://poj.org/problem? Id = 2492

Question:

A boring scientist said that only two bugs of different gender can be combined, of course, without gay. I will give you a couple of bugs that can be together and ask if there is any gay in them.

The first thought of getting this question is to look up the set, the two relationships, and put the bugs of different gender into two different sets. I thought it was not feasible.

For example, the input of 1 2 \ n 3 4, the input of 1 2 is put into two sets, and the input of 3 4 has to be put in two sets, obviously not.

 

Since we cannot immediately determine which set 3 and 4 belong to, can we store them first?

Use an array to record the opposite elements of the subscript Element

Read (x, y );

A [x] = y;

When you read this element again, you can directly merge the elements with different genders of X.

Read (x, z );

Union (Z, a [x]);

According to this idea, the following code is provided:

# Include <cstdio>
# Include <cstring>
Using namespace STD;
Int f [2010];
Int A [2010];
Int n, m;
Bool flag;
Void make_set (){
For (INT I = 1; I <= N; I ++)
F [I] = I;
}
Int find_set (int x ){
If (X! = F [x]) {
F [x] = find_set (F [x]);
}
Return f [x];
}
Void Union (int x, int y ){
X = find_set (X );
Y = find_set (y );
If (X! = Y) f [x] = y;
}
Int main (){
Int T, Ti, I, j, X, Y;
Scanf ("% d", & T );
For (Ti = 1; Ti <= T; Ti ++ ){
Scanf ("% d", & N, & M );
Flag = false;
Memset (A,-1, sizeof ());
For (I = 0; I <= N; I ++)
F [I] = I;
For (I = 0; I <m; I ++ ){
Scanf ("% d", & X, & Y );
If (FLAG) continue;
If (A [x] =-1 ){
If (A [y]! =-1)
Union (X, a [y]);
A [x] = y;
A [y] = X;
}
Else {
If (A [y] =-1 ){
Union (Y, a [x]);
A [x] = y;
A [y] = X;
}
Else {
If (find_set (x) = find_set (y )){
Flag = true;
Continue;
}
Else {
Union (X, a [y]);
Union (Y, a [x]);
}
}
}
}
If (FLAG) printf ("Scenario # % d: \ nsuspicious bugs found! \ N ", Ti );
Else printf ("Scenario # % d: \ Nno suspicious bugs found! \ N ", Ti );
}
Return 0;

}

A bit speculative...

What should we do with orthodox concurrent queries?

Open up an array to store the relationship between x and the root node. R [root] = 0. For subnodes, 0 indicates the same as the root node, and vice versa.

In this way, after reading X and Y, you only need to judge whether FX is equal to FY, and then check whether it is the same-sex. If not, merge.

You can use only one set and use the relational R [] to determine the solution.

 

In this way, the key lies in the update of the relational array R.

There are only two relationships (0, 1), indicating whether the child node is the same as the root node.

1. In find_set (), make sure that the child node has a stable relationship with the root node after the root node changes.

2. When merging, FY is no longer used as the root node, so its R value is determined by the R value of X and Y.

Code for details:

# Include <cstdio>
Using namespace STD;
Int f [2010];
Int R [2010];
Int n, m;
Bool flag;
Int find_set (int x ){
Int temp;
If (x = f [x]) {
Return X;
}
Temp = f [x];
F [x] = find_set (temp );
R [x] = (R [x] + R [temp]) % 2; // maintain the stability of R [x] relative to the root node
Return f [x];
}
Void Union (int x, int y, int FX, int FY ){
F [FY] = FX;
R [FY] = (R [x] + R [y] + 1) % 2;
/*
Here R [FY] and R [FX] are both 0
R [x]-R [FX] If x fx is the same as 0, the opposite sex is 1.
R [y]-R [FY] If y FY is the same as 0, the opposite sex is 1.
When both X and FX, Y and FY are same-sex or both are opposite-sex, the value of R [FY] is 1.
(R [x]-R [FX] + R [y]-R [FY] + 1) % 2
R [FY] = (R [x] + R [y] + 1) % 2;
*/
}
Int main (){
Int T, Ti, I, j, X, Y, FX, FY;
Scanf ("% d", & T );
For (Ti = 1; Ti <= T; Ti ++ ){
Scanf ("% d", & N, & M );
Flag = false;
For (I = 0; I <= N; I ++ ){
F [I] = I;
R [I] = 0;
}
For (I = 0; I <m; I ++ ){
Scanf ("% d", & X, & Y );
If (FLAG) continue;
FX = find_set (X );
FY = find_set (y );
If (FX = FY ){
If (R [x] = R [y]) {
Flag = true;
Continue;
}
}
Else {
Union (X, Y, FX, FY );
}
}
If (FLAG) printf ("Scenario # % d: \ nsuspicious bugs found! \ N ", Ti );
Else printf ("Scenario # % d: \ Nno suspicious bugs found! \ N ", Ti );
}
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.