ZJOI 2004 嗅探器 题解

题目传送门

题目大意: 给出一张无向图以及两个点 s t st e d ed ,找出一个编号最小的并且在所有st 到 ed 的路径上的点。

题解

显然这个点是一个割点嘛。

于是我们找出所有满足要求的割点中编号最小的即可。

具体就看代码吧:

#include <cstdio>
#include <cstdlib>
#define maxn 110

int n,st,ed;
struct edge{int y,next;};
edge e[(maxn*maxn)<<1];
int first[maxn],len=0;
void buildroad(int x,int y)
{
	e[++len]=(edge){y,first[x]};
	first[x]=len;
}
int dfn[maxn],low[maxn],id=0,ans=999999999;
inline int min(int x,int y){return x<y?x:y;}
void dfs(int x,int fa)
{
	dfn[x]=low[x]=++id; int son=0;
	for(int i=first[x];i;i=e[i].next)
	{
		int y=e[i].y;
		if(y==fa)continue;
		if(!dfn[y])
		{
			dfs(y,x); son++;
			if(low[y]<low[x])low[x]=low[y];
			if(low[y]>=dfn[x]&&dfn[y]<dfn[ed]&&dfn[x]<dfn[ed]&&x!=st&&x!=ed)ans=min(ans,x);
//假如满足low[y]>=dfn[x],则x是割点
//假如dfn[y]和dfn[x]都小于dfn[ed],那么说明x和y都在st到ed的路径上,那么x就是一个合法的割点
//注意不能只判断x,只有同时判断了y才能保证x是必须经过的一个割点
		}
		else if(dfn[y]<low[x])low[x]=dfn[y];
	}
}

int main()
{
	scanf("%d",&n);
	int x,y;
	while(scanf("%d %d",&x,&y),x!=0&&y!=0)buildroad(x,y),buildroad(y,x);
	scanf("%d %d",&st,&ed);
	dfs(st,-1);
	if(ans==999999999)printf("No solution");
	else printf("%d",ans);
}
发布了234 篇原创文章 · 获赞 100 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/a_forever_dream/article/details/103055988