1021. Couples
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
N couples are standing in a circle, numbered consecutively clockwise from1to2N. Husband and wife do not always stand together. We remove the couples who stand together untilthe circle is empty or we can't remove a couple any more.
Can we remove all the couples out ofthe circle?
Input
There may be several test cases inthe input file. In each case, thefirst line is an integer N(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 theendofthe input.
Output
Output "Yes"if we can remove all the couples out ofthe circle. Otherwise, output "No".
Sample Input
414235678213240
Sample Output
Yes
No
#include<iostream>#include<stack>usingnamespacestd;
int main()
{
int n;
cin >> n;
while(n != 0)
{
int a,b;
int couple[2*n+1];
for (int i = 1; i <= n; ++i)
{
cin >> a >> b;
couple[a] = couple[b] = i;
}
stack<int> s;
for (int i = 1; i <= 2*n; ++i)
{
if(!s.empty() && s.top() == couple[i])
s.pop();
else
s.push(couple[i]);
}
if(s.empty())
cout << "Yes" << endl;
elsecout << "No" << endl;
cin >> n;
}
return0;
}
1021. CouplesConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionN couples are standing in a circle, numbered consecutively clockwise from 1 to 2N. Husband and wife do not always stand toget