Judging whether the given graph is a spanning tree, if there is a heavy edge is not, if there is no heavy edge but the connected component is greater than 1 is not
The Find function will overflow with an infinite stack with the previous recursive write Orz
Stack overflow, add this string
#pragma COMMENT (linker, "/stack:1024000000,1024000000")
Sample Input
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1-1
Sample Output
Yes
Yes
No
1# include <iostream>2# include <cstdio>3# include <cstring>4# include <algorithm>5# include <cmath>6# include <queue>7# define LLLong Long8 using namespacestd;9 Ten Const intmaxn=100010; One intF[MAXN]; A BOOLVIS[MAXN]; - intSAVE[MAXN]; - BOOLFlag; the intFindintx) - { - while(x!=F[x]) -x=F[x]; + returnx; - } + voidBingintUintv) A { at intt1=find (u); - intT2=Find (v); - if(T1!=T2) f[t1]=T2; - ElseFlag =1;//with heavy edges - } - intMain () in { - //freopen ("In.txt", "R", stdin); to intu, v; + while(SCANF ("%d%d", &u, &v)! =EOF) - { the if(U = =-1&& v = =-1) * Break ; $ if(U = =0&& v = =0)Panax Notoginseng { -printf"yes\n") ; the Continue ; + } A inti; the for(i=1; i<maxn;i++) + { -f[i]=i; $ } $memset (Vis,0,sizeof(Vis)); -F[u] =v; - intL =0 ; theFlag =0 ; - if(!Vis[u])Wuyi { theVis[u] =1 ; -save[l++] =u; Wu } - if(!Vis[v]) About { $VIS[V] =1 ; -save[l++] =v; - } - while(SCANF ("%d%d", &u, &v)) A { + if(U = =0&& v = =0) the Break ; - if(flag) $ Continue ; the if(!Vis[u]) the { theVis[u] =1 ; thesave[l++] =u; - } in if(!Vis[v]) the { theVIS[V] =1 ; Aboutsave[l++] =v; the } the Bing (u,v); the } + intres =0;//Connected Components - for(i =0; I < L; i++) the if(F[save[i]] = =Save[i])Bayires++ ; the if(Res >=2|| Flag = =1) theprintf"no\n") ; - Else -printf"yes\n") ; the } the return 0; the}
View Code
HDU 1272 determines whether the given graph is a spanning tree (and is set)