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

应该是数据太弱了

Posted by Alanqky at 2014-08-01 16:53:00 on Problem 1308
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 100000
int father[N];
int flag[N];
int ok;
void init()
{
  for(int i=0;i<N;i++)
  {
     father[i]=i;
  }
  memset(flag,0,sizeof(flag));
  ok=0;
} 
int find_father(int x)
{
    return father[x]==x?x:father[x]=find_father(father[x]);
}
void merge(int a,int b)
{
   int x=find_father(a);
   int y=find_father(b);
   if(x==y) ok=1;
   else
   {
       if(x>y) father[x]=y;
       else
         father[y]=x;
   }
}
int main(void)
{
    int a,b; 
    int m;
    int t=0;
    while(scanf("%d %d",&a,&b)!=EOF)
    {
    if(a<0&&b<0) break;
      t++;
    if(a==0&&b==0)  
    {
       printf("Case %d is a tree.\n",t);
       continue;
    }
      m=0;
      if(a>m) m=a;
      if(b>m) m=b;
      init();
      flag[a]=1;
      flag[b]=1;
      merge(a,b);
      while(scanf("%d %d",&a,&b))
      {
         if(a==0&&b==0) break;
          flag[a]=1;
          flag[b]=1;
          merge(a,b);
          if(a>m) m=a;
         if(b>m) m=b;
      }
      int cnt=0;
      for(int i=1;i<=m;i++)
      {
         if(flag[i]==1)
         {
           if(father[i]==i) cnt++;
         }
      }
      if(cnt>1) ok=1;
      if(ok==0) printf("Case %d is a tree.\n",t);
      else printf("Case %d is not 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