一道比较喜欢的题。
考虑贪心所以只加入 连边不劣。
考虑当前已顺次遍历 ,设 为 邻域最大编号点。对于点 , 则点 必然在 dfs 栈中, 则点 无贡献。
考虑直接找到最靠后的 点判断其是否与 相邻即可。
#pragma target("avx512f,sse2,sse3,sse4,sse4.2")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=1e6+5,inf=1e6;
int t,n,m;
int stk[N],top,ans,maxx[N],vis[N];
vector<int>v[N];
void work(){
stk[top=1]=1; ans=0;
for(int now=2;now<=n;now++){
for(int to:v[now]) vis[to]=1;
while((top>=2)&&(maxx[stk[top]]<=now-1)) top--;
if(!vis[stk[top]]) ans++;
stk[++top]=now;
for(int to:v[now]) vis[to]=0;
}
printf("%d\n",ans);
}
signed main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
maxx[x]=max(maxx[x],y);
maxx[y]=max(maxx[y],x);}
work();
for(int i=1;i<=n;i++) v[i].clear(),maxx[i]=0;
}
return 0;
}