[Poj 1182] food chain, poj1182 food chain

Source: Internet
Author: User

[Poj 1182] food chain, poj1182 food chain
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

Eat C, C eat.

There are N animals numbered 1-N. Every animal is A, B, and C, but we do not know

Which one is it.

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.

2 x y indicates that X eats Y.

This person speaks K sentences one by one for N animals in the preceding two statements. Some of these K sentences are true.

Some are false. When one sentence meets the following three conditions, this sentence is a lie, otherwise it is the truth.

• The current statement conflicts with some of the preceding actual statements.

• In the current statement, X or Y is greater than N, which is false.

• 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 and K sentences.

Input/Output Format

Input Format:

The first line has two integers, N, K, indicating N animals and K sentences.

The second line begins with one sentence per line (see the example below as required)

Output Format:

A row, an integer, indicates the total number of false statements.

Input and Output sample input sample #1:
100 71 101 12 1 22 2 32 3 31 1 32 3 11 5 5
Output sample #1:
3
Description

1 ≤ N ≤ 5 ≤ 10 ^ 4

1 ≤ K ≤ 10 ^ 5

Question

Create three elements I-A, I-B, and I-C for each animal I, and create and query sets with these 3 x N elements. This query sets the following information for maintenance:

~ I-x indicates "I belongs to Type x"

~ Check each group in the set to indicate that all elements in the group occur simultaneously or not.

For example, if I-A and j-B are in the same group, it indicates that if I belongs to type A, j must belong to Type B. If j belongs to type

B, then I must belong to category. Therefore, you only need to perform the following operations on each piece of information.

~ First, x and y belong to the same type... merge x-A and y-A, x-B and y-B, x-C, and y-C.

~ In the second type, x eats y ........................... merge x-A, y-B, x-B, y-C, x-C, and y-.

However, before merging, you must first determine whether the merger will produce contradictions. For example, in the case of the first type of information,

Check whether x-A, y-B, or y-C is in the same group.

 

1 # include <cstdio> 2 # include <iostream> 3 using namespace std; 4 const int maxn = 50005; 5 const int maxm = 100005; 6 int n, m; int par [maxn * 3], deep [maxn * 3]; // It must be multiplied by 3, where 7 int T [maxm], X [maxm], Y [maxm]; 8 void init (int n) {9 for (int I = 0; I <n; I ++) {10 par [I] = I; 11 deep [I] = 0; 12} 13} 14 int find (int x) {15 if (par [x] = x) return x; 16 else return par [x] = find (par [x]); 17} 18 void unite (int x, int y) {19 x = find (x ), y = find (y); 20 if (x = y) return; 21 if (deep [x] <deep [y]) par [x] = y; 22 else {23 par [y] = x; 24 if (deep [x] = deep [y]) deep [x] ++; 25} 26} 27 bool same (int x, int y) {28 return find (x) = find (y); 29} 30 int read () {// cin, cout on poj T 31 int x = 0, f = 1; char ch = getchar (); 32 while (ch <'0' | ch> '9 ') {if (ch = '-') f =-1; ch = getchar ();} 33 while (ch> = '0' & ch <= '9 ') {x = x * 10 + ch-'0'; ch = getchar ();} 34 return x * f; 35} 36 int main () {37 n = read (), m = read (); 38 for (int I = 0; I <m; I ++) T [I] = read (), X [I] = read (), Y [I] = read (); 39 init (n * 3); 40 int ans = 0; 41 for (int I = 0; I <m; I ++) {42 // cin> T [I]> X [I]> Y [I]; 43 int t = T [I], x = X [I]-1, y = Y [I]-1; 44 if (x <0 | x> = n | y <0 | y> = n) {ans ++; continue;} 45 if (t = 1) {46 if (same (x, y + n) | same (x, y + 2 * n) ans ++; 47 else {48 unite (x, y ); 49 unite (x + n, y + n); 50 unite (x + 2 * n, y + 2 * n ); 51} 52} 53 else {54 if (same (x, y) | same (x, y + 2 * n) ans ++; 55 else {56 unite (x, y + n); 57 unite (x + n, y + 2 * n); 58 unite (x + 2 * n, y ); 59} 60} 61} 62 printf ("% d \ n", ans );
63 return 0; 64}

 

 

 

 

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.