/*
1021. Couples简单题 -> 规模原来很大 -> 难 ->数据结构: 栈
题目大意:
N对夫妇站成一圈
如果某对夫妇站在相邻位置,则从圈中移走
重复以上操作
问最后会不会没人
如1 3是一对,2 4是一对,则No
如1 4是一对,2 3是一对,则Yes
1<=N<=100,000
解题思路:
类似于括号匹配(出题点),可将n对夫妇看成n种括号
用一个栈来模拟,将括号逐个push到栈里
当栈顶存在匹配对时进行pop操作
看最后栈是否为空
*/
#include <iostream>
#include <stack>
using namespace std;
int main(){
int husband;
int wife;
int a[200001];
int n;
while(1){
cin >> n;
if (n==0)
break;
for(int i=1;i<=n;i++){
cin >> husband >> wife;
a[husband]=i;
a[wife]=i;
}
stack <int> couples;
couples.push(a[1]);
//1.重点:采用STL的stack,需要包含头文件 #include <stack>
//2.重点:记住stack的基本函数 pop()、push()、empty()、top(}
for(int j=2;j<=2*n;j++){
if(!couples.empty()&& a[j]==couples.top())
couples.pop();
else
couples.push(a[j]);
}
if(!couples.empty())
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}