Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
这段用的是除根节点外每个节点的入度都为1的性质,0MSIn 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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator