POJ 1182 food chain

Source: Internet
Author: User

Time Limit: 1000 MS Memory Limit: 10000 KTotal Submissions: 32266 Accepted: 9405 Description three types of animals, A, B, and C, in the animal kingdom, constitute an interesting circle. 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 these N animals in the food chain: the first 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 real statements. 2) the current statement is false when X or Y is greater than N. 3) the current statement indicates that X eats X, it 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. The first line of Input 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 has only one integer, indicating the number of false statements. Sample Input 100 71 101 1 2 22 2 3 3 3 1 1 3 3 3 1 1 1 5 5 Sample Output 3 Source Noi 01 write more than 200 lines of code by yourself, the result shows that WAF is indeed able to pass the 60 or 70 code on the Internet. Ah, I have read the solution report for this question and checked the typical ones. We recommend that you read the report: http://blog.csdn.net/c0de4fun/article/details/7318642?cpp] # include <iostream> # include <stdio. h ># include <cstring> using namespace std; int a [50010]; int val [50010]; int main () {int findparent (int x ); void crlcle (int a, int B, int x, int y, int D); int I, j, n, m, s, t, res; int d, x, y, parent_x, parent_y; scanf ("% d ", & n, & m); memset (val, 0, sizeof (val); for (I = 1; I <= n; I ++) {a [I] = I;} res = 0; for (I = 1; I <= m; I ++) {scanf ("% d ", & d, & x, & y); if (x> n | y> n) | (d = 2 & x = y )) {res ++; continue;} parent_x = findparent (x); parent_y = findparent (y); if (parent_x! = Parent_y) {crlcle (parent_x, parent_y, x, y, d);} else {if (d = 1) {if (val [x]! = Val [y]) {res ++ ;}} else {if (3-val [x] + val [y]) % 3! = 1) {res ++ ;}}} printf ("% d \ n", res); return 0 ;}int findparent (int x) {int k1, k2, s, t; k1 = x; s = 0; while (x! = A [x]) {s + = val [x]; x = a [x];} while (k1! = A [k1]) {t = s % 3; s-= val [k1]; val [k1] = t; k2 = a [k1]; a [k1] = x; k1 = k2;} return x;} void crlcle (int c, int B, int x, int y, int d) {int I, j; a [B] = c; val [B] = (3-val [y] + (D-1) + val [x]) % 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.