Spaceless

TzhsOJ P1253 仙人掌

由题意可知,一定存在一条连接i与i+1的边,因为只存在一个简单环,所以除了i到i+1的边之外,只能有一条边覆盖i到i+1这个位置,这样就转成了线段覆盖问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

int n,m,tot,cur;

struct node
{
int x,y;
}e[200003];

bool cmp(const node&s1,const node&s2)
{
return s1.y<s2.y;
}

int main()
{
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);
for(int i=1;i<=t;i++)
if(e[i].x>=cur)
{
cur=e[i].y;
tot++;
}
printf("%d\n",tot+n-1);
return 0;
}