HDU 1325 poj 1308 is it a tree? (Query set)

Source: Internet
Author: User

This question is the bare parallel query set. The key lies in the judgment of several types of trees.

1. Empty tree is a tree 2. Forest is not a tree 3. No ring

Or from the perspective of the inbound degree: 1, no ring; 2, except for the root, all the inbound degrees are 1, root inbound degrees are 0; 3, this structure has only one root, otherwise it will be the forest.


This question was originally made for poj 1308 during the summer vacation, but not for HDU. The empty tree is not considered.

Use the check set to determine the number of forests. Note that the numbers are random, not in order ....

/*input:0 01 1 0 01 2 1 2 0 0 1 2 2 3 4 5 0 01 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 01 2 2 1 0 0 -1 -1output:Case 1 is a tree.Case 2 is not a tree.Case 3 is not a tree.Case 4 is not a tree.Case 5 is not a tree.Case 6 is not a tree.*/#include<math.h>#include<stdio.h>  #include<string.h>#define inf 0x3ffffff#define maxn 10+100000#define max(a,b) ((a)>(b)?(a):(b))int N,M,K;int parent[maxn];int vis[maxn];int find(int *parent,int k){    if(parent[k]==-1)        return k;    parent[k]=find(parent,parent[k]);    return parent[k];}int main(){    int i,j,again;    int e,r,ans,ee,rr,cnt;int nn;    memset(parent,-1,sizeof(parent));    memset(vis,0,sizeof(vis));    nn=ans=again=cnt=0;    while(1)    {        if(again)        {            for(j=0,i=1;i<=nn;i++)            {                if(vis[i] && parent[i]==-1)                    j++;                if(j>1)                {ans=1;break;}            }if(nn==0)ans=0;            if(ans)                printf("Case %d is not a tree.\n",++cnt);                        else                printf("Case %d is a tree.\n",++cnt);            ans=again=0;            memset(parent,-1,sizeof(parent));            memset(vis,0,sizeof(vis));        }        scanf("%d%d",&e,&r);nn=max(nn,max(e,r));        if(e<0 && r<0)            break;        if(e==0  && r==0 )        {again=1;continue;}vis[r]=vis[e]=1;        if(ans==1)            continue;        ee=find(parent,e);        rr=find(parent,r);        if(r!=rr || rr==ee)            ans=1;        parent[rr]=ee;                }   return 0;  }


HDU 1325 poj 1308 is it a tree? (Query set)

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.