The main topic: N (1≤n≤100000) couples form a ring, numbered 1 to 2n. Adjacent couples can be removed from the ring, if you can delete all couples, then output yes, otherwise output No. There are multiple sets of test styles, and when n equals 0 o'clock, the input ends.
Problem-solving ideas: Reverse thinking, in the process of generating loops, instead of deleting the couple after generating the ring.
Data structure used: stack.
The code is as follows:
#include <iostream>#include<stack>#include<map>using namespacestd;intMain () {intn, nn; while(Cin >> N, n! =0) {Stack<int>St; Map<int,int>m; //read into the couple's intW, H; for(inti =0; I < n; i++) {cin>> W >>h; M[W]=h; M[H]=W; } NN= n *2; for(inti =1; I <= nn; i++) {//couple number starting from 1 to n*2 if(!st.empty () && st.top () = =M[i]) {St.pop (); } Else{St.push (i); } } if(St.empty ()) {cout<<"Yes"<<Endl; } Else{cout<<"No"<<Endl; } } return 0;}
SOJ 1021 Couples