SOJ 1021 Couples

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.