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

好不容易用STL写了个,却超时

Posted by sunnvya at 2006-10-03 08:14:46 on Problem 1308
#include<iostream>
#include<iterator>
#include<map>
using namespace std;
int main()
{
    int x,y,j=1;
    map<int,int>tree;
    map<int,int>::iterator p;
    while(cin>>x>>y&&x>=0&&y>=0)
    {
            if(x!=0&&y!=0)
            {tree.insert(make_pair(y,x));}
            else {cout<<"Case "<<j++<<" is a tree."<<endl;tree.clear();continue;}
            bool t=1;
            while(cin>>x>>y&&x!=0&&y!=0)
            {
                if(tree.count(y)==1&&tree[y]!=x)t=0;
                else tree.insert(make_pair(y,x));
            }
            if(!t)
            {cout<<"Case "<<j++<<" is not a tree."<<endl;}
            else
            {
                       int max=0;
                       for(p=tree.begin();p!=tree.end();++p)
                       {
                          int father=p->second;         
                          while(tree[father]!=0)
                          father=tree[father];     
                          tree.erase(father);
                          if(max==0)max=father;
                          else if(max!=father){cout<<"Case "<<j++<<" is not a tree."<<endl;break;}
                          
                       }
                       if(p==tree.end())
                       {cout<<"Case "<<j++<<" is a tree."<<endl;}
                               
            }
            tree.clear();
    }
    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