蒟蒻求助 93分,#11 WA
查看原帖
蒟蒻求助 93分,#11 WA
931707
017_007楼主2023/6/25 20:28
#include<bits/stdc++.h>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;

#define ll long long

const int N = 3e4;

int n,T,dist[N],in[N];
int cnt,first[N];
struct Q {
	int x,y;
}a[N];
struct node{
	int to,nxt;
}edges[N*2010];
bool d[N];
queue<int>sx;

bool cmp(const Q &a,const Q &b) {
	if (a.x!=b.x) return a.x<b.x;
	else return a.y<b.y;
}

void add(int u,int v) {
	edges[++cnt].to=v;
	edges[cnt].nxt=first[u];
	first[u]=cnt;
}

void build() {
	for (int i=1;i<=n;++i) {
		for (int j=i+1;j<=n;++j) {
			if (a[j].x<=a[i].y+1) add(i,j),in[j]++;
			if (a[j].y>=T) add(j,n+1);
		}
	}
}

void bfs() {
	memset(dist,20,sizeof(dist));
	for (int i=1;i<=n;++i) {
		if (a[i].x==1) sx.push(i),dist[i]=1,d[i]=true;
	}
	while (!sx.empty()) {
		int root=sx.front();sx.pop();
		for (int t=first[root];t;t=edges[t].nxt) {
			int h=edges[t].to;
			dist[h]=min(dist[h],dist[root]+1);
			in[h]--;d[h]=true;
			if (!in[h]) sx.push(h);
		}
	}
}

int main(){
	scanf("%d%d",&n,&T);
	for (int i=1;i<=n;++i) scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+1+n,cmp);
	if (a[1].x!=1) {
		printf("-1\n");
		return 0;
	}
	if (a[n].y<T) {
		printf("-1\n");
		return 0;
	}
	build();
	bfs();
	if (!d[n+1]) printf("-1\n");
	else printf("%d\n",dist[n+1]-1);
	return 0;
}

2023/6/25 20:28
加载中...