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