Food chains
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total submissions: 26553 |
|
accepted: 7718 |
There are three types of animal a,b,c in the Description animal kingdom, and the food chain of these three animals constitutes an interesting ring. A eat B, B eat c,c eat a.
Existing n animals, numbered in 1-n. Every animal is one of the a,b,c, but we don't know what it is.
There are two ways to describe the food chain that n animals form:
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 sayings, one sentence after another to say K sentence, this k sentence some is true, some false. When a sentence satisfies one of the following three, this sentence is a lie, otherwise it is the truth.
1 The current words and some of the previous real words conflict, is a lie;
2 in the current words, x or y is larger than N, is a lie;
3 The current words that x Eat x, is a lie.
Your task is to output the total number of lies based on the given n (1 <= n <= 50,000) and K sentences (0 <= k <= 100,000).
The first line of Input is two integers n and K, separated by a single space.
The following k lines are three positive integers d,x,y, and two numbers are separated by a space, where D represents the type of argument.
If d=1, it means x and Y are homogeneous.
If d=2, it means x eats y.
Output has only one integer, indicating the number of lies.
Sample Input
7
1 1
2 1 2
2 2 3 2
3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
The following:
The three categories of animals A, B, c form the food chain cycle, telling two animals of the relationship (similar or natural enemies), to determine the relationship is the previous conflict.
Analysis:
And check the set of advanced applications.
Suppose Father[a]=b, Rank[a]=0 said A and B, Rank[a]=1 said B can eat a;rank[a]=2 said a can eat B.
For a set of data D x y, if X is the same as the root node of Y, use (rank[y]-rank[x]+3)%3!=d-1 If the unequal description is untrue;
If x is different from the Y root node, it means that the two are not in the same set and are grouped. Let X's root fx become the root fy of Y's parent node (FATHER[FY]=FX), the value of rank[fy] needs to be carefully summed up.
The source code is as follows:
#include <stdio.h> #include <memory.h> #define MAXN 50001 int FATHER[MAXN],RANK[MAXN];
void Init (int n) {int i;
for (i=1;i<=n;i++) father[i]=i;
memset (rank,0,sizeof (rank));
int find_set (int x) {if (x!=father[x]) {int fx=find_set (father[x)); Rank[x]= (Rank[x]+rank[father[x]])%3;
Attention is rank[father[x]] rather than rank[fx] father[x]=fx;
return father[x];
BOOL Union (int x,int y,int type) {int fx,fy;
Fx=find_set (x);
Fy=find_set (y); if (fx==fy) {if ((rank[y]-rank[x]+3)%3!=type) return true;
This relationship can be obtained by example else return false;
} father[fy]=fx; rank[fy]= (rank[x]-rank[y]+type+3)%3;
Different from the upper-style need to be carefully summed up return false;
int main () {freopen ("In.txt", "R", stdin);
int n,k;
int sum,i;
int d,x,y;
scanf ("%d%d", &n,&k);
cin>>n>>k;
Init (n);
sum=0;
for (i=0;i<k;i++) {scanf ("%d%d%d", &d,&x,&y); cin>>d>>x>>y; Use CIN to timeout if (x>n | | y>n | |
(x==y && d==2))
sum++; else if (Union (x,y, d-1))//d-1 convenient expression sum++;
printf ("%d\n", sum);
return 0; }