就是括号匹配的题目,如果有交集就是NO
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<stack>
#include<algorithm>
using namespace std;
stack<int> s;
int a[200005];
int main()
{
int t,n,m,temp1,temp2,i;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
memset(a,0,sizeof (a));
while(!s.empty()) s.pop();
for(i=1;i<=m;i++)
{
scanf("%d%d",&temp1,&temp2);
a[temp1]=i;a[temp2]=i;
}
for(i=1;i<=2*n;i++)
{
if(a[i]==0) continue;
if(s.empty()) s.push(a[i]);
else
{
if(a[i]==s.top()) s.pop();
else s.push(a[i]);
}
}
if(s.empty()) printf("YES
");
else printf("NO
");
}
}