Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

come on

Posted by houyideng at 2015-03-31 20:05:04 on Problem 1308
/*
不一定要用并查集,把关系处理好就可以了
可以利用图的度的性质来进行即可
1.空值也为树
2.重边不是树
3.根节点只有一个
4.不能自身指向自身
5.保证联通即入度为0的点只能有
*/
#include<stdio.h>
#include<string.h>
#define N  11000
#define inf 0x3fffffff
int vis[N];
int degree[N];
int Min(int v,int vv) {
return v>vv?vv:v;
}
int Max(int v,int vv) {
return v>vv?v:vv;
}
int main() {
    int a,b,aa,bb,minn,maxx,cnt,i,k=0,flag,cur;
    while(scanf("%d%d",&a,&b)!=EOF) {
            if(a==0&&b==0) {
                printf("Case %d is a tree.\n",++k);
                continue;
            }
            else
            if(a<=0||b<=0)
            return 0;
        memset(vis,0,sizeof(vis));
        memset(degree,0,sizeof(degree));
       flag=0;minn=inf;maxx=-1;
        degree[b]++;
        vis[a]=vis[b]=1;
        if(a==b)
            flag=1;
        minn=Min(a,minn);
        minn=Min(b,minn);
        maxx=Max(a,maxx);
        maxx=Max(b,maxx);
        while(scanf("%d%d",&a,&b),a||b) {
            if(a==b)
                flag=1;
            degree[b]++;
            vis[a]=vis[b]=1;
            minn=Min(a,minn);
            minn=Min(b,minn);
            maxx=Max(a,maxx);
            maxx=Max(b,maxx);
        }
        if(flag) {
            printf("Case %d is not a tree.\n",++k);
            continue;
        }
        cnt=0;
        for(i=minn;i<=maxx;i++)  {
            if(vis[i]&&degree[i]==0)
            cnt++;
          // printf("%d %d\n",i,degree[i]);
        }
           // printf("%d\n",cnt);
        cur=0;
        for(i=minn;i<=maxx;i++)
            if(vis[i]&&degree[i]>1)
            cur++;
        if(cnt==1&&cur==0) {
            printf("Case %d is a tree.\n",++k);
            continue;
        }
          printf("Case %d is not a tree.\n",++k);
    }
return 0;}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator