跳到正文

题解

P4700

cwfxlh约 4 分钟
原文
文章信息与版本

文章信息

发布
2023年5月11日
收录
2026年10月18日
更新
2026年10月22日
篇幅
约 4 分钟
获赞
7
题目
P4700

版本

  1. v1当前

P4700

提供一种不需要缩点的做法。

对于任何起点都不能到达的终点,将其删去,那么剩下的点就有一个性质:对于点 xx,它所能到达的终点在 yy 的值上形成一段连续区间。

用反证证明一下:如果对于起点 aa,终点 bb,cc,dd,yb>yc>ydy_b>y_c>y_d 且 aa 可以到达 bb,dd,不能到达 cc,则一定有另外一个起点 ee 可以到达点 cc,不妨令 ee 在 aa 的上方,那么考虑 ee 到 cc 的路径与 aa 到 bb 的路径,二者必定有交点(显然),并且根据题目限制,交点一定是一个端点 ff,那么 aa 可以到 ff,ff 又可以到 cc,那么 aa 可以到 cc,假设不成立,结论得证。

有了这个结论,我们只需要计算每个点到终点的最大最小 yy 值就好了,如果正着做需要缩点(但是我是凸轮废物所以不会),考虑对终点遍历,一遍求最大,一遍求最小就可以了。

上代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,a,b,x[500003],y[500003],mx[500003],mn[500003],vis[500003];
int k1,k2,k3,k4,k5,k6,k7,k8,k9;
vector<int>r;
vector<int>st;
vector<int>ed;
vector<int>E[500003];
vector<int>F[500003];
vector<pair<int,int> >ans;
void dfs(int now){
	vis[now]=1;
	for(auto i:E[now])if(!vis[i])dfs(i);
	return;
}
bool comp(int XX,int YY){return y[XX]>y[YY];}
int q[2000003],totq;
signed main(){
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	for(int i=1;i<=n;i++){
		scanf("%lld%lld",&x[i],&y[i]);
		mn[i]=-1;
	}
	for(int i=1;i<=m;i++){
		scanf("%lld%lld%lld",&k1,&k2,&k3);
		E[k1].emplace_back(k2);
		F[k2].emplace_back(k1);
		if(k3==2){
			F[k1].emplace_back(k2);
			E[k2].emplace_back(k1);
		}
	}
	for(int i=1;i<=n;i++)if(vis[i]==0&&x[i]==0)dfs(i);
	for(int i=1;i<=n;i++){
		if(x[i]==0)st.push_back(i);
		if(x[i]==a&&vis[i])ed.push_back(i);
	}
	sort(st.begin(),st.end(),comp);
	sort(ed.begin(),ed.end(),comp);
	for(int i=0;i<ed.size();i++){
		totq=0;
		q[++totq]=ed[i];
		mx[ed[i]]=i;
		for(int j=1;j<=totq;j++){
			for(auto u:F[q[j]]){
				if(mx[u]>0)continue;
				mx[u]=i+1;
				q[++totq]=u;
			}
		}
	}
	for(int i=ed.size()-1;i>=0;i--){
		totq=0;
		q[++totq]=ed[i];
		mx[ed[i]]=i;
		for(int j=1;j<=totq;j++){
			for(auto u:F[q[j]]){
				if(mn[u]>0)continue;
				mn[u]=i+1;
				q[++totq]=u;
			}
		}
	}
	for(auto i:st)printf("%lld\n",max(mn[i]-mx[i]+1,0ll));
	return 0;
}
原文
存档评论3 条抓取于