codevs-1074-food chain-and check

Source: Internet
Author: User

Describe

Three kinds of animals a eat B, B eat C, C eat a.

1, A, b for the same kind

2, A, b means a eats B.

Judging the number of false lies


Analysis

    • Noip saw before, not at that time, now it is a bit abstract to understand.
    • Combination of weighted values and check set
    • Can determine the relationship of X, Y's find to the same ancestor.
    • PA[X] is the ancestor of X, R[x] is the distance from X to the ancestor, there are three kinds, 0, 1, 2.  0 The expression and ancestor homogeneous, 1 means can eat ancestors, 2 means to be eaten. R[find (x)] must be 0 I verified it with an assert.
    • To find the first record of the X current ancestor px, when x and PX initially determine the relationship r[px] must be 0. The value of r[px] after the recursive find (PX) is the distance from PX to the current ancestor, and X and PX are now the same ancestor, so the distance from X to the original PX is the distance from the PX to the current ancestor.
    • When merging X, y, first px = find (x), py = Find (y). For example, to set PA[PX] as a py, the PY will not change, consider the PX rank how to change. If the merge requires X, y similar, then x to the py distance should be equal to the r[y], then r[x] + r[px] = = R[y], so r[px] = R[y]-r[x]. This way, after the path is compressed, the new r[x] is equal to r[y].


codevs-1074-food chain-and check

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.