food chain
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 57387 |
|
Accepted: 16781 |
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 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
Ideas: And look at the set. Detailed Review Challenge Program Design Contest (second edition p89)
A rough idea, each animal has three states, respectively, with the number of i,i+p,i+2p;
If I,j is the same, then there are i,j in the same set, i+p the same set as J+p, i+2p,j+2p the same set,
If I eat J, there is i,j+p the same set, i+p,j+2p the same set, i+2p,j the same set.
So if J, eat Z, according to this merger, you can get z, eat i;
So every question is judged to be contradictory.
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <stdlib.h>5#include <string.h>6 Const intn=5*30005;7 using namespacestd;8 intdian[3*N];9 intquan[3*N];Ten intMainvoid) One { A inti,j,k,p,q; - intx1,x2,x3; -scanf"%d%d",&p,&q); the { - for(i=0; i<3*n; i++) - { -quan[i]=1; +dian[i]=i; - } + intCnt=0; A while(q--) at { -scanf" %d%d%d",&x1,&x2,&x3); - intxx1,yy1; - intXx2,yy2; - intXx3,yy3; - for(xx1=x2;dian[xx1]!=xx1;) inxx1=dian[xx1]; - for(yy1=x3;dian[yy1]!=yy1;) toyy1=Dian[yy1]; + for(xx2=x2+p;xx2!=DIAN[XX2];) -Xx2=dian[xx2]; the for(yy2=x3+p;yy2!=Dian[yy2];) *Yy2=Dian[yy2]; $ for(xx3=x2+2*p;xx3!=DIAN[XX3];)Panax Notoginsengxx3=dian[xx3]; - for(yy3=x3+2*p;yy3!=Dian[yy3];) theyy3=Dian[yy3]; + if(x2<=0|| x3<=0|| x2>p| | X3>p) A { thecnt++; + } - Else ${if(x1==1) $ { - if(xx1!=yy1&&xx1!=yy2&&yy1!=xx2) - { the if(quan[xx1]>=Quan[yy1]) - {Wuyiquan[xx1]+=Quan[yy1]; thedian[yy1]=xx1; -quan[xx2]+=Quan[yy2]; Wudian[yy2]=xx2; -quan[xx3]+=Quan[yy3]; Aboutdian[yy3]=xx3; $ } - Else - { -quan[yy1]+=quan[xx1]; Adian[xx1]=yy1; +quan[yy2]+=quan[xx2]; thedian[xx2]=Yy2; -quan[yy3]+=quan[xx3]; $dian[xx3]=Yy3; the } the } the Else if(XX1==YY1) {Continue;} the Elsecnt++; - } in if(x1==2) the { the if(xx1!=yy1&&xx1!=yy2&&yy1!=xx2) About { the if(quan[xx1]>=Quan[yy2]) the { thequan[xx1]+=Quan[yy2]; +dian[yy2]=xx1; -quan[xx2]+=Quan[yy3]; thedian[yy3]=xx2;Bayiquan[xx3]+=Quan[yy1]; thedian[yy1]=xx3; the } - Else - { thequan[yy2]+=quan[xx1]; thedian[xx1]=Yy2; thequan[yy3]+=quan[xx2]; thedian[xx2]=Yy3; -quan[yy1]+=quan[xx3]; thedian[xx3]=yy1; the } the }94 Else if(xx1==yy2) the { the Continue; the }98 Elsecnt++; About } - 101}}printf ("%d\n", CNT);102 103}return 0;}
Food Chain (poj1182)