POJ 1182: Food chain

Source: Internet
Author: User

and check Set

The idea is simple.

Open two arrays

Eating and being eaten

But it blew me off the whole afternoon.

Mlgb

Put up an error code

Dinner's gone.

Bother


#include <stdio.h> #include <string.h> #include <algorithm>using namespace std;const int maxn=55555; int Fa[maxn];int eat[maxn];int ate[maxn];int Find (int x) {return fa[x]==x?x:fa[x]=find (fa[x]);} void merge (int x,int y) {int fx,fy; fx=find (x); Fy=find (y); if (Fx!=fy) fa[fy]=fx;}    int main () {int n,k;    int com,n1,n2;    scanf ("%d%d", &n,&k);            for (int i=1;i<=n;i++) {eat[i]=0;            ate[i]=0;    Fa[i]=i;    } int num=0;            for (int i=0;i<k;i++) {scanf ("%d%d%d", &com,&n1,&n2); if (com==1) {if (n1>n| |                N2>n) {num++;continue;} if (Find (N1) ==find (eat[n2]) | |                Find (N2) ==find (Eat[n1]) {num++;continue;} if (! (                        eat[n1]==0&&ate[n2]==0) {if (Find (Eat[n1]) ==find (ATE[N2]))                {num++;continue;} }//if (Find (N2) ==find (atE[N1]) | |                        Find (N1) ==find (ATE[N2])//{num++;continue;}            Merge (N1,N2); } else if (com==2) {if (n1>n| | N2>n) {num++;continue;} else//{if (n1==n2) {num++;continue                        ;} if (Find (N1) ==find (n2)) {num++;continue;}                        if (Find (Ate[n1]) ==find (n2))//{num++;continue;}                          if (Find (N1) ==find (Eat[n2])) {num++;continue;} if (! (                        eat[n1]==0&&ate[n2]==0) {if (Find (Eat[n1]) ==find (ATE[N2]))                {num++;continue;} } if (! ( eat[n1]==0&&eat[n2]==0)//if (Eat[n1]==find (eat[n2)) | | Eat[n2]==find (Eat[n1]))                       {if (find (Eat[n1]) ==find (EAT[N2]))                        {num++;continue;}                        } if (eat[n1]==0) eat[n1]=n2;                        else merge (EAT[N1],N2);                        if (ate[n2]==0) ate[n2]=n1;               else merge (ATE[N2],N1);    }}} printf ("%d\n", num); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1182: Food chain

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.