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 ;}