Sicily 1021 Couple

Source: Internet
Author: User

1021. Couples Constraints

Time limit:1 secs, Memory limit:32 MB

Description

N couples is 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 was empty or we can ' t remove a couple any more.

Can We remove all the couples out of the circle?

Input

There may is several test cases in the input file. In each case, the first line was an integern(1 <= N <= 100000)----The number of couples. In the following N lines, each line contains a 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

The idea is simple, give the relationship between the husband and wife, and then judge will be from 1 to 2N in a ring from the husband and wife according to the relationship between a pair of the move out until the ring is empty.

First prepare an array of arr to store conjugal relationships, such as a and b for couples, then there are arr[a]=b and arr[b]=a.

Then create a stack st to store the elements, and then from 1 to 2N to put the elements into the stack, if the top element of the stack and the element to be placed in a couple relationship, that is, in the ARR array has a corresponding relationship, then the top element of the stack is moved out, the element will be placed skip no longer put, the equivalent of moving the

This loop continues until all elements are traversed, and if the stack is empty at this point, the required requirements can be achieved, the output is "Yes", and the output "no" is not possible.

The code is as follows:

#include <iostream> #include <stack>using namespace Std;int arr[200002];//store couple corresponding number 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;        }        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;}                                 

  

Sicily 1021 Couple

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.