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

谁来看看偶的代码,自己找的数据都过了,还是WA,请路过的大牛看看啊!(有简单注释)

Posted by nenu10_wangd at 2010-08-18 10:05:30 on Problem 1308
#include<stdio.h>
#include<string.h>
//#include<conio.h>
int main(){
    int n,m;
    int pre[21000]={0}; //存放每个节点的前驱 
    bool mark[21000]={false};//mark【】标记那个点是否是树的节点(包括根节点) 
    int flag=1;//标记是否是树 
    int cnt=0;//输出case %d 时用 
    int vexnum=0, edge=0;//分别记录节点和边数目 
    while(scanf("%d%d",&n,&m)){
        if(n==-1&&m==-1) break;
        
        if(n==0&&m==0) {
           cnt++;int i;
           int count=0;//记录根节点的数目 
           if(vexnum-1!=edge) flag=0;
           
          else{ 
                 for(i=1;vexnum;i++){
                     if(mark[i]==1) {
                                if(pre[i]==0)count++;            
                                      vexnum--;
                     }
                  }
           if(count!=1) flag=0;//如果不是只有一个根节点 ,不是树 
           
           }
           
           if(vexnum==0&&edge==0){ printf("Case %d is a tree.\n",cnt); continue;}//空的也是树 
           
           if(flag)
           printf("Case %d is a tree.\n",cnt);
           else printf("Case %d is not a tree.\n",cnt);
           /*还原到初始值*/ 
           flag=1;
           memset(pre,0,sizeof(pre));
           memset(mark,false,sizeof(mark));
           vexnum=0, edge=0;
           continue;
        }
        
        edge++;
        if(mark[n]==0){ vexnum++; mark[n]=1;}
        if(mark[m]==0){ vexnum++; mark[m]=1;}
     if(pre[m]==0) pre[m]=n;//记录前驱 
     else flag=0;   //如果一个点前驱不唯一,不是树 
                               
                               
                               
    }
    
    //getch();
    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