也是老师要求的六题之一.... 不过这题没有用栈来做... 链接: http://soj.me/1021 // source code of submission 437964, Zhongshan University Online Judge System #include<iostream> #include<map> using namespace std; class node { public: node(int x) { key=x; pre=NULL; next=NULL; } int key; node* pre; node* next; }; class list { public: list() { beg=NULL; end=NULL; } node* beg; node* end; void insert(int x) { node* tmp=new node(x); tmp->pre=end; tmp->next=beg; end->next=tmp; beg->pre=tmp; end=tmp; } void debeg() { if(beg==end) { beg=NULL; end=NULL; return; } beg=beg->next; beg->pre=end; end->next=beg; } void deend() { if(beg==end) { beg=NULL; end=NULL; return; } end=end->pre; beg->pre=end; end->next=beg; } }; int main() { int c; while(cin>>c) { if(c==0) { return 0; } map<int,int> maps; for(int i=0;i<c;i++) { int m,n; cin>>m>>n; maps[m]=n; maps[n]=m; } list lis; for(int i=1;i<=2*c;i++) { if(lis.beg==NULL) { node* tmp=new node(i); lis.beg=tmp; lis.end=tmp; continue; } else if(maps[i]==lis.end->key) { lis.deend(); } else { lis.insert(i); } } if(lis.beg==NULL) { cout<<"Yes"<<endl; continue; } else { for(int i=1;i<=2*c;i++) { if(lis.end!=lis.beg) { cout<<"No"<<endl; goto here; } else { lis.debeg(); lis.deend(); } } if(lis.beg==NULL) { cout<<"Yes"<<endl; continue; } } here:; } return 0; }