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

HDU上都过了,而且讨论区给的样例都能过还是Wa

Posted by AK47biubiubiu at 2016-07-13 16:14:31
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100000+10;
int v[N],f[N],a[N];
int find(int x)
{
    return f[x]==-1?x:f[x]=find(f[x]);
}
int main()
{
    int x,y,i=0,t=1;
    while(~scanf("%d%d",&x,&y)&&x>0&&y>0)
    {
        if(x==0&&y==0)
        {
            printf("Case %d is a tree.\n",t++);
            continue;
        }
        int ff=0,k=0;
        memset(f,-1,sizeof(f));
        memset(a,0,sizeof(a));
        memset(v,0,sizeof(v));
        int xx=find(x);
        int yy=find(y);
        if(xx!=yy)
            f[xx]=yy;
        else
            ff=1;
        a[k++]=x,a[k++]=y;
        v[y]++;
        for(i=1;; i++)
        {
            scanf("%d%d",&x,&y);
            if(x==0&&y==0) break;
            int xx=find(x);
            int yy=find(y);
            if(xx!=yy)
                f[xx]=yy;
            else
                ff=1;//自己指向自己或有多条路到达某个点;
            a[k++]=x,a[k++]=y;
            v[y]++;
            if(v[y]>1) ff=1;//除根节点每个节点只有一条被指向边;
        }
       if(ff) printf("Case %d is not a tree.\n",t++);
       else
       {
           sort(a,a+k);
           int kk=unique(a,a+k)-a;
//           for(i=0;i<kk;i++)
//            printf("%d ",v[a[i]]);
           for(i=0;i<kk;i++)
           if(v[a[i]]!=1)
            ff++;
           if(ff!=1)
           printf("Case %d is not a tree.\n",t++);
           else
            printf("Case %d is a tree.\n",t++);
       }
    }
   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