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 lihuiyykl at 2009-08-20 12:55:32 on Problem 1308
/*
此方法既不省时间,也不省内存 
*/
#include<iostream>

using namespace std;

#define MAX 20000000
int Father[MAX];
bool flag[MAX];

void init()
{
     for(int i=1;i<=MAX;i++) Father[i]=i;
     memset(flag,false,sizeof(flag));
}

int Find(int n)
{
    if(Father[n]!=n) return Find(Father[n]);
    return n;
}

int main()
{
    int a,b;
    int k=1;
    while(cin>>a>>b)
    {
          init();
          if(a==-1&&b==-1) break;
          Father[b]=a;
          flag[a]=true;
          flag[b]=true;
          bool ans=true;
          while(cin>>a>>b)
          {
              if(a==0&&b==0) break;
              if(flag[a]==true&&flag[b]==false)  Father[b]=a;
              else if(flag[a]==false&&flag[b]==true)  Father[a]=b;
              else if(flag[a]==true&&flag[b]==true)
              {
                  int tempa=Find(a),tempb=Find(b);
                  if(tempa==tempb) ans=false;
                  else  Father[tempb]=tempa;
              }
              flag[a]=true;
              flag[b]=true;
          }
          if(ans) cout<<"Case "<<k++<<" is a tree."<<endl;
          else cout<<"Case "<<k++<<" is not a tree."<<endl;
    }
}

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