"Go" poj-1308-is It A Tree?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.