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

这段用的是除根节点外每个节点的入度都为1的性质,0MS

Posted by speedcell4 at 2011-08-02 10:57:00 on Problem 1308 and last updated at 2011-08-02 10:57:17
In Reply To:贴两段代码 Posted by:speedcell4 at 2011-08-02 10:56:12
#include<iostream>
#define SIZE 10000
using namespace std;
int x,y;
int n,times=1,a[SIZE]={0},b[SIZE]={0};
void Init(void)
{
    n=0;
    memset(a,0,sizeof(a));
}
void Union(int x,int y)
{
    a[y]++;
}
int cmp(void const *a,void const *b)
{
    return *(int *)a-*(int *)b;
}
bool check(int n)
{
    qsort(b,n,sizeof(int),cmp);
    int count=(a[b[0]]==0);
    for(int i=1;n-i>0;i++)
    {
        if(b[i]>b[i-1]&&a[b[i]]!=1) count++;
    }
    return count==1;
}
int main()
{
    while(Init(),scanf("%d %d",&x,&y),x!=-1||y!=-1)
    {
        while(x||y)
        {
            b[n++]=x;
            b[n++]=y;
            Union(x,y);
            scanf("%d %d",&x,&y);
        }
        if(check(n)) printf("Case %d is a tree.\n",times++);
        else printf("Case %d is not a tree.\n",times++);
    }
    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