Portal for Poj 1198
Food chains
Time limit:1000ms Memory limit:10000k
Total submissions:53343 accepted:15629
Description
There are three types of animal a,b,c in the animal kingdom, and the food chain of these three animals is an interesting ring. A eat B, B eat c,c eat a.
Existing n animals, numbered with 1-n. Every animal is one of the a,b,c, but we don't know what it is.
Some people describe the food chain relationship between these n animals in two ways:
The first argument is "1 x y", which means that x and Y are homogeneous.
The second argument is "2 x y", which means x eats y.
This person to n animals, with the above two statements, a sentence after sentence to say K sentence, this k sentence some is true, some false. When one sentence satisfies one of the following three, the sentence is a lie, otherwise it is the truth.
1) The current words conflict with some of the preceding words, which is false;
2) The current word in x or y is greater than N, is a lie;
3) The current words say x eats x, is a lie.
Your task is to output the total number of falsehoods according to the given N (1 <= n <= 50,000) and the K-sentence (0 <= K <= 100,000).
Input
The first line is two integers n and K, separated by a single space.
The following k lines each line is three positive integer d,x,y, and two numbers are separated by a space, where D denotes the kind of claim.
If d=1, it means that x and Y are homogeneous.
If d=2, it means x eats y.
Output
There is only one integer that represents the number of false lies.
Sample Input
100 7
1 101 1
2 1 2
2 2 3
2 3 3
1 1 3
2 3 1
1 5 5
Sample Output
3
Problem solving ideas: take right and check the set:
Grandpa father son son and grandpa
0 0 (i + j)%3 = 0
0 1 (i + j)%3 = 1
0 2 (i + j)%3 = 2
1 0 (i + j)%3 = 1
1 1 (i + j)%3 = 2
1 2 (i + j)%3 = 0
2 0 (i + j)%3 = 2
2 1 (i + j)%3 = 0
2 2 (i + j)%3 = 1
See the code for details:
#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intMAXN =50005;intFA[MAXN], RANK[MAXN];voidInit (intx) { for(intI=1; i<=x; i++) {Fa[i] = i;//rank[i] = 0;}memset(Rank,0,sizeof(rank));}intFind (intx) {if(fa[x]! = x) {intFX = Find (fa[x]); RANK[X] = (rank[x]+rank[fa[x])%3; FA[X]=FX; }returnFA[X];}BOOLUnion (intXintYintType) {intFX = Find (x);intFY = Find (y);if(FX = = FY) {if((rank[y]-rank[x]+3)%3! = Type)return true;return false; } Fa[fy] = FX; Rank[fy] = (rank[x]-rank[y]+3+type)%3;return false;}intMain () {//Note Do not use multiple sets of inputs intN,m,d,x,y;scanf("%d%d", &m, &n);intsum =0; Init (m); while(n--) {scanf("%d%d%d", &d,&x,&y);if(x>m | | y>m | | (X==y && d==2)) sum++;Else if(Union (x, Y, D1)) sum++; }printf("%d\n", sum);return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Poj 1182 food Chain