POJ 1182 food chain with right and check

Source: Internet
Author: User

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).

If Father[a]=b, rela[a]=0 represents a and B similar; Rela[a]=1 says B can eat a;rela[a]=2 that a can eat B.

Then it is to enumerate all the circumstances, you can deduce the formula, it is best not to write each weight separately, otherwise it will be chaotic.

The first belt right and check set, feel not easy to push the formula, out of the results, natural easy to use.

#include <cstdio>#include<cstring>#include<vector>#include<cmath>#include<stack>#include<cstdlib>#include<queue>#include<map>#include<iostream>#include<algorithm>using namespaceStd;typedefLong LongLL;intN,k,ans;intd,x,y;intfather[50100],rela[50100];intFINDFA (intX//to find the ancestor, by the way, to clarify the relationship .{    if(x!=Father[x]) {        intZU=FINDFA (Father[x]);//Find AncestorsRela[x]= (Rela[x]+rela[father[x])%3;//after enumeration, you can derivefather[x]=zu; }    returnfather[x];}intCombineintOpintXinty) {    intZUX=FINDFA (x);//to find the ancestor, by the way, the relationship between X and ancestor    intZuy=findfa (y);//to find the ancestors, and to clarify the relationship between Y and ancestors    if(Zux==zuy)//in a system    {        if((rela[y]-rela[x]+3)%3!=OP)return false;//after enumeration, you can derive        Else return true; }    Else//is two systems, it will be able to pass the relationship between the rotation of the docking{Father[zuy]=zux;//let the Fathers on theRela[zuy]= (rela[x]-rela[y]+op+3)%3;//after enumeration, you can derive        return true; }}intMain () {scanf ("%d%d",&n,&k);  for(intI=1; i<=n;i++) Father[i]=i;  while(k--) {scanf ("%d%d%d",&d,&x,&y); if(x>n| | y>n| | (d==2&&x==y)) ans++; Else if(!combine (d1, x, y)) ans++; } printf ("%d\n", ans); return 0;}

POJ 1182 food chain with right and check

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.