Test instructions: Judging if the graph is a tree
Link: Point Me
This problem with and check the set to determine connectivity, connectivity and only 1 into the degree of 0, the remaining penetration is 1, is the tree
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8 using namespacestd;9 #defineMOD 1000000007Ten Const intinf=0x3f3f3f3f; One Const Doubleeps=1e-5; A #defineCL (a) memset (A,0,sizeof (a)) - #defineTS printf ("*****\n"); - Const intmaxn=1000000; the intN,m,tt; - intA[MAXN]; - intF[MAXN]; - int inch[MAXN]; + BOOLVIS[MAXN]; - intFindintx) + { A if(f[x]==-1)returnx; at Else returnf[x]=find (F[x]); - } - voidBingintXinty) - { - intt1=find (x); - intT2=find (y); in if(t1!=T2) -f[t1]=T2; to } + intMain () - { the inti,j,k; * #ifndef Online_judge $Freopen ("1.in","R", stdin);Panax Notoginseng #endif - intCnt=0; the intu,v; + intIcase=0; A BOOLflag=true; thememset (f,-1,sizeof(f)); +memset (Vis,false,sizeof(Vis)); -Memsetinch,0,sizeof(inch)); $ while(SCANF ("%d%d", &u,&v)! =EOF) $ { - if(u==-1&&v==-1) Break; - if(u==0&&v==0) the { -icase++;Wuyiprintf"Case %d", icase); the intt0=0; - for(i=0; i<cnt;i++) Wu { - if(Find (A[i])!=find (a[0])) About { $flag=0; - Break; - } - if(inch[a[i]]==0) t0++; A Else if(inch[a[i]]>1) + { theflag=0; - Break; $ } the } the if(t0!=1) flag=false; the if(cnt==0) flag=true; the if(flag) printf ("Is a tree.\n"); - Elseprintf"is not a tree.\n"); inCnt=0; thememset (f,-1,sizeof(f)); thememset (Vis,false,sizeof(Vis)); AboutMemsetinch,0,sizeof(inch)); theflag=true; the } the Else + { - if(!Vis[u]) the {Bayivis[u]=1; thea[cnt++]=u; the } - if(!Vis[v]) - { thevis[v]=1; thea[cnt++]=v; the } the if(Find (U) ==find (v)) flag=0; - Else the { the Bing (u,v); the inch[v]++;94 } the } the } the}
HDU 1325 Judging if the graph is a tree