This reference to: Luo Gu p2024 solution to a problem
Title Description
There are three types of animals in the animal kingdom A, B, C, three types of animal food chain constitute an interesting ring. A food B, B
Eat C, C eat A.
Animals prior N, 1 - id N. Each animal is A, B, C in kind, but we do not know
Which in the end is which.
Some people describe this relationship N animal food chain formed by two different ways:
The first argument is "1 XY", represents the X and Y are similar.
The second argument is "2 XY", X represents eat Y.
This person for N animals, with the above two statements, one sentence by sentence to say K, K which some true sentence
, Some false. When one of the following three words, this sentence is a lie, the truth is otherwise.
• The current case with some true words of earlier conflicts, is a lie
• if the current X or Y greater than N, that is a lie
• Current eat as saying X X, is a lie
Your task is given according to the total number of N and K words, the output of lies.
Input Format
The input data from eat.in
The first line of two integers, N, K, N expressed animal, K words.
Each word line the second line (in accordance with the requirements of the subject, see examples)
Output Format
Output in the eat.out
Line, an integer representing the total number of lies.
Sample input and output
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
3
Description / Tips
1 ≤ N ≤ 5 ∗ 10^4
1 ≤ K ≤ 10^5
First encountered such a problem, really frustrated that he probably thought to be divided into three races, but does not know how to maintain. When God saw a large valley on the Los solution to a problem, really sobering. The original three times with disjoint-set can easily solve the immediate problem of the maintenance of such inter-ethnic relations.
This question can be used as a template of such inter-ethnic relations to use
Probably thinking: double the deposit with the race, keep prey twice, three times the deposit predators. Defined find (x) for x ancestors
1 // If x, y same race, the same with the race race, x, y prey same race, x, y predators same race 2 the Join (x, y) . 3 the Join (X + n-, Y + n-) . 4 the Join (x + 2 * n-, Y + 2 * n-) . 5 . 6 Find (x) == Find (Y) // XY same race . 7 Find (+ n-x) == Find (Y) // Y is a prey of x, i.e. x y eat relationship . 8 Find (x + 2 * n-) == Find (y) // x prey y, i.e. x, y relationship between eating
AC Code
#include <bits / STDC ++ H.> the using namespace STD; const int NN = 5E4 + 10 ; int n-, m; int A [ . 3 * NN]; int Find ( int X) { // find the common ancestor IF (X! = A [X]) A [X] = Find (A [X]); return A [X]; } void the Join ( int X, int Y) { // combined set int FX = Find (X), FY = Find (Y); IF ! (FX = FY) { A [FX] = FY; } } int main () { CIN >> >> n- m; for ( int I = . 1 ; I <= . 3 * n-; I ++) A [I] = I; // disjoint-set operation before initialization int T, X, Y; int coun- = 0 ; // number of errors the while (M-- ) { Scanf ( " % D% D% D " , T &, & X, & Y); IF (X> n- Y ||> n-) { // an error condition coun- ++ ; Continue ; } IF(T == . 1 ) { IF (Find (n-X +) == Find (y) || Find (X + 2 * n-) == Find (y)) { // X and y unusual race relations coun- ++ ; } the else { the Join (x, y); the Join (X + n-, Y + n-); the Join (X + 2 * n-, Y + 2 * n-); // let the x, y co-ethnic, x, y prey same race, x, y predators same race } } the else { IF (X == Y) { // an error condition coun- ++ ; } the else IF (Find (X) == Find (Y) || Find (X +2* n-) == Find (y)) { // Non x eat y relationship coun- ++ ; } the else { the Join (x + n-, y); the Join (x + 2 * n-, y + n-); the Join (x, y + 2 * the n-); // who eats whom relations } } } cout << coun << endl; return 0 ; }