Poj 1182 food chain)

Source: Internet
Author: User

 

Food Chain

Time limit:1000 ms
Memory limit:10000 K

Total submissions:44835
Accepted:13069

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

Source

Noi 01

 

Conclusion: Use a set to maintain the established relationships

/** Create three elements A, B, and C for each animal. The positions are I, I + N, I + 2 * n * I-X indicates that I belongs to the Type X *. In the query set, each group indicates that all elements in the group occur simultaneously or not. */# include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> using namespace STD; const int maxn = 50000*3 + 5; const int maxk = 100000 + 5; int P [maxn]; void make_set () {memset (p,-1, sizeof (p);} int find_set (int x) {return P [x] =-1? X: P [x] = find_set (P [x]);} void union_set (int x, int y) {int FX = find_set (x ), FY = find_set (y); If (FX = FY) return; P [FX] = FY;} bool same (int x, int y) {return find_set (X) = find_set (y);} int main () {int N, K; scanf ("% d", & N, & K); int D, X, y; int ans = 0; make_set (); For (INT I = 0; I <K; I ++) {scanf ("% d ", & D, & X, & Y); If (x> N | Y> N | (D = 2 & X = y) {ans ++; continue;} X --; y --; If (D = 1) // x y belongs to the same class {If (same (X, Y + n) | same (X, Y + 2 * n) ans ++; else {union_set (x, y); union_set (x + N, Y + n); union_set (x + 2 * n, Y + 2 * n) ;}} else if (D = 2) {// X eat y if (same (x, y) | same (X, Y + 2 * n) ans ++; else {union_set (X, Y + n); union_set (x + N, Y + 2 * n ); union_set (x + 2 * n, Y) ;}} printf ("% d \ n", ANS); Return 0 ;}

 

I found another solution on the Internet, but it is not very understandable. I will study it later:

Http://endless.logdown.com/posts/2014/04/10/find-sets-notes-differential-statistics-maintenance

Http://blog.csdn.net/synapse7/article/details/18517109

Http://blog.csdn.net/ditian1027/article/details/20804911

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.