Poj 1182-food chain and query set

Source: Internet
Author: User

Source: http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1182

 

Solution report:

 

This is another question to be combined, but it is a little more complicated than the previous two. After a long time, I searched for other problem-solving reports and found a clever method.

 

Record a kind [I] for each node I represents the relationship between node I and its parent node P [I ].

 

Kind [I] = 1 parent eat child (P [I] Eat I) kind [I] = 2 child eat parent (I eat P [I]) kind [I] = 0 (I and P [I)

 

Therefore, makeset, findset, and unionset

 

There should be:

Makeset: Initialize P [I] = I, kind [I] = 0 rank [I] = 0

 

Findset: as shown in the following example, the path is compressed, but the parent node of the node also changes accordingly. Therefore, the relationship between the node and its parent node also changes, that is, kind [I] should also change, but the specific relationship can be derived based on the relationship between the node and its original parent node and the relationship between its original parent node and its original parent node, that is

Kind [a] = (kind [a] + kind [p [a]) % 3

 

Unionset: combines two sets. The root node of one set points to the root node of another set. Therefore, the kind of the root node of one set also needs to be changed accordingly, the details can be derived based on the relationship between each node. However, I obtained through enumeration and finding the rule ....

 

Input d x y each time

For the node x y that you encounter, first determine whether they belong to the same set. If they belong to, the relationship between them is determined. Then, you can directly determine whether the given D satisfies the relationship between them.

If the node x y is not in a set, their relationship is uncertain. Therefore, this sentence is true and we need to record their relationship, that is, unionset is called to merge the collection of the two nodes. In this way, the relationship between the nodes is determined for the new collection.

 

# Include <iostream> <br/> using namespace STD; </P> <p> int * P; <br/> int * rank; <br/> int * kind; // relationship with the parent node <br/> // kind [p [I]-kind [I] = 0; = 1 parent; = 2 sub-users </P> <p> void makeset (int A) <br/>{< br/> P [a] =; <br/> rank [a] = 0; <br/> kind [a] = 0; <br/>}</P> <p> int findset (int) // modify kind <br/>{< br/> int temp; <br/> if (! = P [a]) <br/>{< br/> temp = findset (P [a]); <br/> kind [a] = (kind [p [a] + kind [a]) % 3; <br/> P [a] = temp; <br/>}< br/> return P [a]; <br/>}</P> <p> void Exchange (Int & X, Int & Y) <br/>{< br/> int temp = x; <br/> X = y; <br/> Y = temp; <br/>}</P> <p> void _ Union (int A, int B, int D) <br/>{< br/> int roota = findset (a); <br/> int rootb = findset (B ); </P> <p> If (rank [rootb] <rank [roota]) <br/> {<br/> Exchange (roota, rootb ); <br/> Exchange (a, B); <Br/> d = 2-D; <br/>}</P> <p> P [roota] = rootb; // rootb becomes the representative of the new set, kind [roota] needs to be modified <br/> If (rank [rootb] = rank [roota]) <br/> rank [rootb] ++; </P> <p> switch (d) <br/>{< br/> case 0: // B eat a <br/> kind [roota] = (kind [B]-kind [a] + 1) % 3; <br/> break; <br/> case 1: // a, B <br/> kind [roota] = (kind [B]-kind [a] + 3) % 3; <br/> break; <br/> case 2: // a eat B <br/> kind [roota] = (kind [B]-kind [a] + 2) % 3; <br/> break; <br/>}</P> <p> int main () <br />{< Br/> int N, K; <br/> int n = 0; <br/> CIN> N> K; <br/> P = new int [n + 1]; <br/> Rank = new int [n + 1]; <br/> kind = new int [n + 1]; <br/> for (INT I = 1; I <= N; I ++) <br/> makeset (I); <br/> int D, X, Y; <br/> while (k --) <br/>{< br/> scanf ("% d", & D, & X, & Y ); <br/> If (x> N | Y> N) <br/> N ++; <br/> else <br/> {<br/> If (D = 2 & X = y) <br/> N ++; <br/> else <br/> {<br/> If (findset (x) = findset (y )) <br/> {<br/> If (kind [y]-kind [x] + 1-D) % 3! = 0) <br/> N ++; <br/>}< br/> else <br/> _ Union (X, Y, d ); <br/>}< br/> cout <n <Endl; <br/> return 0; <br/>}

 

Appendix:

 

Food Chain
Time limit:1000 ms   Memory limit:10000 K
Total submissions:18371   Accepted:5208

Description

The animal kingdom contains three types of animals A, B, and C. The food chains of these three types constitute an interesting ring. A eats B, B eats C, and C eats.
There are n animals numbered 1-n. Every animal is one of A, B, and C, but we don't know which one it is.
There are two ways to describe the relationship between the food chains of the N animals:
The first statement is "1 x Y", indicating that X and Y are similar.
The second statement is "2 x Y", which indicates that X eats y.
This person speaks K sentences one by one for N animals in the preceding two statements. These K sentences are true or false. When one sentence meets the following three conditions, this sentence is a lie, otherwise it is the truth.
1) The current statement conflicts with some of the preceding actual statements;
2) In the current statement, X or Y is greater than N, which is false;
3) The current statement indicates that X eats X, which is a lie.
Your task outputs the total number of false statements based on the given n (1 <= n <= 50,000) and K statements (0 <= k <= 100,000.

Input

The first line is two integers N and K, separated by a space.
Each row in the following K rows contains three positive integers, D, X, and Y, which are separated by a space. D indicates the type of the statement.
If D = 1, X and Y are of the same type.
If D = 2, X eats y.

Output

Only one integer indicates the number of false statements.

Sample Input

100 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5

Sample output

3

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.