这个说必须加入所有的点,也就是i,i+1之间一定有边,否则没法做,如果这样就必须选上所有点,在区间覆盖,就行了
dp方法。。。
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
int n,m;
int f[199999],g[199999];
int main(){freopen("cactus.in","r",stdin);freopen("cactus.out","w",stdout);scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);if(x>y)swap(x,y);if(x+1==y)continue; g[y]=max(g[y],x);}f[0]=-1;for(int i=2;i<=n;i++)f[i]=max(f[i-1],f[g[i]]+1);printf("%d",f[n]+n-1);
}
贪心方法
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 100010
using namespace std;
int n,m;
struct node
{int x,y;
};node e[N*2];
bool cmp(const node&s1,const node&s2)
{return s1.y<s2.y;
}
int main()
{//freopen("cactus.in","r",stdin);//freopen("cactus.out","w",stdout);scanf("%d%d",&n,&m);int t=0;for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);if(x>y)swap(x,y);if(x+1!=y)e[++t].x=x,e[t].y=y;}sort(e+1,e+t+1,cmp);int tot=0,p=0;for(int i=1;i<=t;i++)if(e[i].x>=p)p=e[i].y,tot++;printf("%d",tot+n-1);return 0;
}