And query set exercises --- poj 1182 food chain

Source: Internet
Author: User

Typical query questions.

It mainly maintains the relationship between nodes.

First, let's look at the path compression section:


[Cpp]
Int find (int x)
{
If (fa [x] = x) return x;
Int t = fa [x];
Fa [x] = find (fa [x]);
R [x] = (r [x] + r [t]) % 3;
Return fa [x];
}
Save the original parent node.
Let's see how to merge:


[Cpp]
Fx = find (x); fy = find (y );
If (fx! = Fy)
{
Fa [fy] = fx;
C --;
R [fy] = (r [x]-r [y]-c + 6) % 3;
}
You can find the rule by enumerating some situations. As for why to add 6, it is to ensure that it is positive (because of-c, it cannot only add 3)
Finally, determine whether it is true:


[Cpp]
If (c = 1) ans + = r [x]! = R [y];
Else ans + = (r [x]-r [y] + 3) % 3! = 1;
This solves the problem.

 

 

Author: ascii991

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.