本題可以不使用對組,因爲這段時間正在學習stl,所以用了對組,純屬練手
AC代碼:
#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std;
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,b,z,x,i,flag=0;
scanf("%d%d",&a,&b);
stack<int> p[3];
pair<int,int> c[b];
for(i=a;i>0;i--)
{
p[0].push(i);
}
for(i=0;i<b;i++)
{
scanf("%d%d",&z,&x);
c[i]=make_pair(z-1,x-1);
}
for(i=0;i<b;i++)
{
if(p[c[i].first].empty())
{
flag=1;
break;
}
else if(!p[c[i].second].empty()&&p[c[i].second].top()<p[c[i].first].top())
{
flag=1;
break;
}
else
{
p[c[i].second].push(p[c[i].first].top());
p[c[i].first].pop();
}
}
if(flag==0)
{
printf("legal\n");
}
else
{
printf("illegal\n");
}
for(i=0;i<2;i++)
{
while(!p[i].empty())
{
p[i].pop();
}
}
}
return 0;
}