好题。
考察对问题的深入了解以及对传统算法的创新
题意很简单,就是求到所有路径中最小的必经点
首先注意到一个性质:必经点一定是所有割点的其中一个。
证明:如果必经点不是割点,那么肯定还存在另外一条路能通过某个割点到达。
于是我们便知道,本题要用求割点。
我们可以对进行改进:
从点出发进行!。
有这样一个性质:如果我们以一个信息中心为根开始搜索,找到一个非根的割点;此时若对应的子树根v的时间戳小于等于的时间戳,则说明b存在于v的子树内。
为什么呢?
由于随序更新,若还没搜到,则其为0;或者若不为0而小于,则说明在进入以前已经被搜到了,除去这两种情况之外,就有一定在的子树内。
那么如果把断掉,的整棵子树都会与根失去联系,和也就一定失去联系。就是所求的点之一。
算法只对原函数的判断条件略做了修改,因此效率得到了极大保留,时间复杂度。
code:
#include<bits/stdc++.h>
#define int long long
#define reg register
using namespace std;
const int maxn=4e5+5;
const int maxm=1e6+5;
const int mod=1e9+7;
const int INF=2e9;
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*10+ch-'0';
ch=getchar();
}
return x*f;
}
int n,m;
struct node
{
int to;
int nxt;
}edge[maxm];
int wugui=1;
int head1[maxn];
void add(int x,int y)
{
wugui++;
edge[wugui].to=y;
edge[wugui].nxt=head1[x];
head1[x]=wugui;
}
int low[maxn];
int dfn[maxn];
int cut[maxn];
int cnt=0;
int ss,tt;
void tarjan(int x)
{
low[x]=dfn[x]=++cnt;
for(reg int i=head1[x];i;i=edge[i].nxt)
{
int t=edge[i].to;
if(dfn[t]==0)
{
tarjan(t);
low[x]=min(low[x],low[t]);
if(low[t]>=dfn[x] && x!=ss && dfn[tt]>=dfn[t])
cut[x]=1;
}
else
low[x]=min(low[x],dfn[t]);
}
}
//int ss,tt;
signed main()
{
n=read();
int x,y;
while(cin>>x>>y)
{
if(x==0 && y==0)
break;
add(x,y);
add(y,x);
}
ss=read(),tt=read();
tarjan(ss);
for(reg int i=1;i<=n;i++)
{
if(cut[i])
{
printf("%lld\n",i);
return 0;
}
}
printf("No solution\n");
//return 0;
return 0;
}
/*
5 7 1
1 2 4
1 4 4
2 5 1
2 3 2
3 4 2
4 2 2
5 3 3
*/