Three days in a row to do and check the set, one day, low efficiency ah ...
And after the collection is the smallest spanning tree after the shortest path, go back to school before the basic algorithm of graph to go through it.
Back to school also review DSA because the school is the B Pat.
Of course, English and fitness can not fall.
The question to be considered is where
① Empty Tree
② without ring
③ Non-Forest
④1 1 0 0 Not a tree cannot point itself to itself
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int M, N;const int size=100 ; int pre[size+1];int visit[size+1];void Init () {for (int i=1; i<size+1; i++) pre[i]=i; memset (Visit, 0, sizeof (Visit));} int getpre (int a) {if (pre[a]!=a) Pre[a]=getpre (Pre[a]); return pre[a];} BOOL Union (int a, int b) {a=getpre (a); B=getpre (b); if (a==b) return false; Pre[a]=b; return true;} int main () {int A, B; BOOL Istree=true; int case=1; while (scanf ("%d%d", &a, &b)//The first pair {if (a==-1) of each set of data break; if (a==0&&b==0) {//Empty tree printf ("Case%d is a tree.\n", case++); Continue } Init (); Visit[a]=visit[b]=1; Istree=true; if (a==b) Istree=false; Else Union (A, b); while (scanf ("%d%d", &a, &b)) {if (a==0&&b==0) Break Visit[a]=visit[b]=1; if (! Union (A, b)) Istree=false; } int setcnt=0; for (int i=1; i<size+1; i++) if (visit[i]&&pre[i]==i) setcnt+ +; if (setcnt>1) Istree=false; if (istree) printf ("Case%d is a tree.\n", case++); else printf ("Case%d was not a tree.\n", case++); } return 0;}
"Go" poj-1308-is It A Tree?