1021. Couples

Source: Internet
Author: User

1021. Couples

Description

N couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. husband and wife do not always stand together. we remove the couples who stand together until the circle is empty or we can't remove a couple any more.

Can we remove all the couples out of the circle?

Input

There may be several test cases in the input file. In each case, the first line is an integerN(1 <= n <= 100000) ---- the number of couples. In the following n lines, each line contains two integers ---- the numbers of each couple.
N = 0 indicates the end of the input.

Output

Output"Yes"If we can remove all the couples out of the circle. Otherwise, output"No".

Sample Input

41 42 35 67 821 32 40
Sample output

YesNo
Problem Source

Zsuacm team member

# Include <iostream> # include <stack> using namespace STD; int arr [200002]; // enter the corresponding numbers of the couple: int main () {int N, A, B; while (CIN> N & N) {stack <int> st; For (INT I = 0; I <n; I ++) {CIN> A> B; arr [a] = B; arr [B] = A; // in this way, there are only two couples, convenience for subsequent comparisons} For (INT I = 1; I <= 2 * n; I ++) {If (! St. empty () & arr [I] = ST. top () ST. pop (); else St. push (I);} If (St. empty () cout <"yes" <Endl; else cout <"no" <Endl;} return 0 ;}

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.