Poj 1182 food chain

Source: Internet
Author: User

Question:


Food Chain
Time limit:1000 ms   Memory limit:10000 K
Total submissions:44323   Accepted:12937

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

Solution:

This is a classic question in the query set. It should also belong to the grouping and query set (type and query set ). For this question, we can understand that three sets A, B, and C are created to indicate that a and a are elements of the same type, set B represents the same type of elements as "elements a eats", and set C represents the same type of elements as "elements eating. The detailed explanation is attached to the code.

Code:

# Include <cstdio> # include <cstring> const int maxn = 5e4 + 10; int set [3 * maxn]; int find (int p) {If (set [p] <0) return P; return set [p] = find (set [p]);} void join (int p, int q) {P = find (P), q = find (Q); If (P! = Q) set [p] = Q ;}int main () {int n, m; scanf ("% d", & N, & M ); memset (set,-1, sizeof (SET); int D, X, Y, CNT = 0; while (M --) {scanf ("% d", & D, & X, & Y ); if (x> N | Y> N | (2 = D & X = y) // If X or Y is greater than N, it is a false statement, if X is used to eat X, it is a lie; {CNT ++; continue;} If (x = y) continue; // If X and X are of the same type, the truth is true, switch (d) {Case 1: // indicates that X and Y are of the same type. {// if y and x + N or Y and x + 2 * n are of the same type, then, X and Y cannot be of the same type. This sentence is false. // If X and Y + N or X and Y + 2 * n are of the same type, then X and Y cannot be of the same type. This sentence is a false statement. If (find (x + n) = find (y) | find (x + 2 * n) = find (y) | find (x) = find (Y + n) | find (x) = find (Y + 2 * n )) {CNT ++; continue;} join (x, y); join (x + N, Y + n); join (x + 2 * n, Y + 2 * n); break;} Case 2: // say that X eats y, that is, Y and x + N are of the same type, X and Y + 2 * n are of the same type {// if y and X, or Y and x + 2 * n are of the same type, y cannot be of the same type as X + N, this sentence is a false statement // If X and Y or X and Y + N are of the same type, X cannot be of the same type as Y + 2 * n, this is a false statement. If (find (x) = find (y) | find (x + 2 * n) = find (y) | find (X) = find (Y + n) {CNT ++; continue;} join (X, Y + 2 * n); join (x + N, y ); join (x + 2 * n, Y + n); break ;}} printf ("% d \ n", CNT); 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.