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
北京大学《ACM/ICPC大学生程序设计竞赛训练》暑期课面向全球招生!

该题根本不用并查集。树的性质。。附AC代码,,

Posted by lvweihua at 2017-08-12 19:49:31 on Problem 1308
/*水题,根本用不着并查集。。。树的性质:结点的个数==边的个数+1  空树的时候单独判断就行了。。23333333333
*/
#include<cstdio>     //顶点的个数==边的个数+1
#include<cstring>
const int MAXN=1000000;
int par[MAXN];
int main()
{
    int m,n;
    int t=1;
    while(scanf("%d%d",&m,&n)&&m>=0&&n>=0){
        memset(par,0,sizeof(par));
        int e=1;
        if((m==0&&n==0)){   //空树
            printf("Case %d is a tree.\n",t++);
            continue;
        }
        par[m]=par[n]=1;
        while(scanf("%d%d",&m,&n)&&m>0&&n>0){
            e++;
            par[m]=par[n]=1;
        }
        int v=0;
        for(int i=0;i<MAXN;i++)
            v+=par[i];
        int flag=(v==e+1);
        printf("Case %d ",t++);
        if(flag)
            puts("is a tree.");
        else
            puts("is not a tree.");
    }
    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