"Original" POJ-----1182 food chain Problem Solving report

Source: Internet
Author: User

Title Address:

http://poj.org/problem?id=1182

Topic content:

Food chains
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 48791 Accepted: 14222

Description

There are three types of animal a,b,c in the animal kingdom, and the food chain of these three animals is an interesting ring. A eat B, B eat c,c eat a.
Existing n animals, numbered with 1-n. Every animal is one of the a,b,c, but we don't know what it is.
Some people describe the food chain relationship between these n animals in two ways:
The first argument is "1 x y", which means that x and Y are homogeneous.
The second argument is "2 x y", which means x eats y.
This person to n animals, with the above two statements, a sentence after sentence to say K sentence, this k sentence some is true, some false. When one sentence satisfies one of the following three, the sentence is a lie, otherwise it is the truth.
1) The current words conflict with some of the preceding words, which is false;
2) The current word in x or y is greater than N, is a lie;
3) The current words say x eats x, is a lie.
Your task is to output the total number of falsehoods according to the given N (1 <= n <= 50,000) and the K-sentence (0 <= K <= 100,000).

Input

The first line is two integers n and K, separated by a single space.
The following k lines each line is three positive integer d,x,y, and two numbers are separated by a space, where D denotes the kind of claim.
If d=1, it means that x and Y are homogeneous.
If d=2, it means x eats y.

Output

There is only one integer that represents the number of false lies.

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

Source

Noi 01 Problem-solving ideas: A well-liked food chain, is worthy of Noi's topic, indeed very difficult. The general direction is to use the right and check set to do, or see a merge one, see two merge a pair, and then update the node weights. If two nodes are already in the collection, and the accumulated node weights and the given relationship contradiction, then even a mistake. Of course, out of scope is also wrong. Well, let's define the right to take it. Define array Num[n], where num[i] (i < N) means the first animal relationship to its root node。 If it is 0, then I and the root node are the same species, if it is 1, then I am eaten by the root node, if it is 2, then I eat root node. Strictly speaking, at any one time, Num[i] can only guarantee that the stored value is the relationship between I and the previous node, and does not necessarily preserve the relationship with the root node. But because before querying num[i], we would call the Find_root method, which would direct I to the root node and maintain the value of num[i], so we can assume that Num[i] maintains the relationship between I and the root node. The next question is how we update the value of Num[i] in Find_root. First, we have to solve a problem. What are the relationships between A and b known and B and C, and the relationship between A and C? That is, for example: a----> B----> C (meaning A is eaten by B, B is eaten by C) 1 1 easy to see, a----> C is also equal to 2, that is, A is eaten by B, B is eaten by C, a can eat C sign convention: may be false We have 3 animals. A, B, c,a, and B are related to x, whereas the relationship between B and C is Y, then the relationship between A and C is [X][y]. There are: [0][1] = 1[0][0] = 0[0][2] = 2 [1][0] = 1[1][1] = 2[1][2] = 0 [2][0] = 2[2][1] = 0[2][2] = 1 It can be seen that the relationship between a and C is actually equal to (x + y)% 3. Therefore, in the recursion of Find_root, we have completed an update to the node on the current node, we can use this relationship, the previous node as a bridge, to update the current node to the root node after the weight. There are several formulas that need to be used: if the value of a----> B is known to be x, then the value of B----> A is (3-x)% 3. If a----> b----> C----> The relationships of neighboring nodes in D are known, then we can ask A to D based on a----> b----> C, then the problem is converted to a----> C----> D, You can use that formula to find the relationship between A and d. All code:
#include <stdio.h>intanimal[50001 ];intrelation[50001 ];intn,k;//1 is eaten by the root node, 0 is the same as the root node, 2 means eating root nodeintFind_root (intChild ) {  if(animal[child] = =0 )    returnChild ; intTMP =animal[Child]; animal[Child]=Find_root (animal[child]); relation[Child]= (relation[child] + relation[TMP])%3; returnanimal[Child];}intUnion_set_and_judge (intOneintBoth,intrel) {  if(rel = =1) rel=0; intFAT1 =Find_root (one); intFAT2 =Find_root (both); if(FAT1! =fat2) {animal[FAT1]=Fat2; intFat2one = (3-relation[one])%3; intFat2two = (fat2one + rel)%3; intFat2fat = (fat2two + relation[))%3; relation[FAT1]=Fat2fat;    return 0; }  intFat2two = (3-relation[])%3; intOne2two = (relation[one] + fat2two)%3;  returnOne2two = = rel?1:2;}intMainvoid) {scanf ("%d%d", &n, &k); intCount =0;  for(inti =0; I < K; i + + ) {    intD,x,y; scanf ("%d%d%d", &d, &x, &y); if(X > N | | y >N) {count++; Continue; }    intFlag =Union_set_and_judge (x, y, D); if(Flag = =2) {Count++; }} printf ("%d\n", Count); return 0;}

"Original" POJ-----1182 food chain Problem Solving report

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.