【Loj #10101. 「一本通 3.6 练习 2」嗅探器】题解

题目链接

首先如果一个点满足答案,则这个点一定是割点。

然后我们可以从 \(a\) 点开始搜,对于每一个点,如果 \(b\) 点在它的儿子内,说明这个点分离了 \(a\)\(b\)

如何判断 \(b\) 是否在它的儿子内,只需要在搜索这个儿子前后判断一下即可。

Code

// Problem: P5058 [ZJOI2004]嗅探器
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5058
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define M 500010
//#define mo
#define N 200010
struct node
{
	int x, y, n; 
}d[M*2]; 
int n, m, i, j, k; 
int dep[N], f[N], h[N], zi[N]; 
int u, v, ans, a, b; 

void cun(int x, int y)
{
	d[++k].x=x; d[k].y=y; 
	d[k].n=h[x]; h[x]=k; 
}

void dfs(int x)
{
	int flag=0; 
	for(int g=h[x]; g; g=d[g].n)
	{
		int y=d[g].y; 
		if(!dep[y])
		{
			int flg=(dep[b]!=0 ? 0 : 1); 
			dep[y]=dep[x]+1; 
			dfs(y); 
			if(dep[f[y]]>=dep[x])
			{
				++zi[x]; 
				if(dep[b]) flag+=flg; 
			}
			if(dep[f[y]]<dep[f[x]]) f[x]=f[y]; 
		}
		else if(dep[y]<dep[f[x]]) f[x]=y; 
	}
	// printf("%lld: %lld %lld %lld\n", x, f[x], zi[x], flag); 
	if(x==b||x==a) flag=0; 
	if(!(zi[x]>1||(x!=a&&zi[x]))) flag=0; 
	if(flag) 
	{
		ans=min(ans, x); 
		// printf("%lld\n", x);
	}
	
}

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	n=read(); ans=0x3f3f3f3f; 
	for(i=1; i<=n; ++i) f[i]=i; 
	u=read(); v=read(); 
	while(u) cun(u, v), cun(v, u), u=read(), v=read(); 
	a=read(); b=read(); 
	dep[a]=1; dfs(a); 
	// for(i=1; i<=n; ++i) printf("%lld ", f[i]); 
	if(ans==0x3f3f3f3f) printf("No solution"); 
	else printf("%lld", ans);
	return 0; 
}

posted @ 2021-11-29 21:32  zhangtingxi  阅读(95)  评论(0编辑  收藏  举报