Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
好不容易用STL写了个,却超时#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator