According to the 3 non-conformance conditions given by the topic, judging whether the chart conforms to these 3 conditions can be
1. Cannot appear internal ring, topological sort judgment
2. Cannot have more than 1 points in the degree of 0, because there is only one root
3. A maximum of one entry per point
One of the points to note here is that the number of this point is random, so the maximum value is 8, but in fact there is not necessarily 8 points, here a maximum value of the parameter, and a total number of points of the parameter to judge can
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 Const intN =100005;7 int inch[N], First[n], K, MAXN, Del, sum;//MAXN records The total number of points, Del Records the number of deleted points8 9 structedge{Ten inty, Next; One}e[n<<2]; A - voidAdd_edge (intXinty) - { theE[k].y = y, E[k].next =First[x]; -FIRST[X] = k++; - } - + voidTuopu (intsrc) - { +del++; A for(inti = first[src]; i!=-1; I=E[i].next) { at intv =e[i].y; - inch[v]--; - if(!inch[v]) Tuopu (v); - } - } - in intMain () - { to //freopen ("a.in", "R", stdin); + intx, y, cas =0; - while(~SCANF ("%d%d", &x, &y)) { the if(X <0&& y <0) Break; * $memset (First,-1,sizeof(first));Panax NotoginsengMemsetinch, -1,sizeof(inch)); -K =0, MAXN =0, sum =0;//sum indicates the total number of points the intCNT =0, root, flag =0;//CNT records How many nodes can be used as vertices, flag as the judge whether a little more than 1 degrees in + while(X! =0|| y!=0){ A Add_edge (x, y); theMAXN =Max (max (MAXN, x), y); + if(inch[X] <0)inch[X] =0, sum++; - if(inch[Y] <0)inch[Y] =0, sum++; $ inch[y]++; $ if(inch[Y] >=2){ -Flag =1; - } thescanf"%d%d", &x, &y); - }Wuyi //cout<< "MAXN:" <<maxn<<endl; the if(flag) { - //cout<< "a bit more than two degrees" <<endl; Wuprintf"Case%d was not a tree.\n", ++CAs); - Continue; About } $ for(inti =1; I<=MAXN; i++) - if(!inch[i]) { -Root =i; -cnt++; A if(cnt>=2) Break; + } the if(CNT >=2){ - //cout<< "has more than two points to serve as roots" <<endl; $printf"Case%d was not a tree.\n", ++CAs); the Continue; the } thedel =0; the Tuopu (root); - if(Del <sum) in { the //cout<< "appearance of inner ring" <<endl; theprintf"Case%d was not a tree.\n", ++CAs); About } the Else theprintf"Case %d is a tree.\n", ++CAs); the } + return 0; -}
HDU 1325 topology sequencing