C++栈问题求助各位大神,急用,谢谢啦!!!
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 are multiple test cases. The first line contains an integer T, indicating the number of test cases. In each case, the first 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.
Output
Output "Yes" if we can remove all the couples out of the circle. Otherwise, output "No".
Sample Input
2
4
1 4
2 3
5 6
7 8
2
1 3
2 4
Sample Output
Yes
No
我的代码:
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int n,i,c,k,j,a[100000][2],n2;
stack<int>couple;
cin>>c;
for (i=0;i<c;i++)
{
while(!couple.empty())
{
couple.pop();
}
cin>>n;
for (j=0;j<n;j++)
cin>>a[j][1]>>a[j][2];
couple.push(1);
n2=n*2;
for (j=2;j<=n2;j++)
{
for (k=0;k<n;k++)
{
if ((couple.top()==a[k][1]&&j==a[k][2])||(couple.top()==a[k][2]&&j==a[k][1]))
{
couple.pop();
continue;
}
else if (k==n)
couple.push(j);
}
}
if (couple.empty())
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
不知道问什麽总是运行出错;