您的位置:首页 > 产品设计 > UI/UE

Hdu 3686 Traffic Real Time Query System(双联通分量+LCA)

2016-09-01 17:09 477 查看
题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3686
思路:对于两条边, 若其在同一双联通分量中,则它们最少有两条路可达。所以从边x--->y,必须经过的点的数目为从x所在双连通分量到y所在双连通分量的割点的数目。则找出所有双连通分量,缩成一点,构成双连通分量---割点----双连通分量。。。。。则缩完点后,新图构成一棵树,则必须经过的点为从x在新图中的点到y在新图中的点的路径上割点的数目,由于树中的边组成都为u---割点---v---割点(割点交替出现),所以路径割点数目为两点距离除以2。

#include<stack>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const int maxn=2e5+50;
stack<int> s;
int cut[maxn];
vector<int> g[maxn];
int n,m,tot,dfs_clock;
int iscut[maxn],scc_cnt;
int low[maxn],dfn[maxn];
int vis[maxn],sccno[maxn];
int anc[maxn][20],d[maxn];
int to[maxn],nt[maxn],info[maxn];
void addedge(int u,int v)
{
to[tot]=v,nt[tot]=info[u],info[u]=tot++;
to[tot]=u,nt[tot]=info[v],info[v]=tot++;
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++dfs_clock;
int child=0;
for(int i=info[u]; i!=-1; i=nt[i])
{
int v=to[i];
if(vis[i]) continue;
vis[i]=vis[i^1]=1;
s.push(i);
if(!dfn[v])
{
++child;
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v])
{
iscut[u]=1;
++scc_cnt;
for(;;)
{
int tmp=s.top();
s.pop();
sccno[tmp]=sccno[tmp^1]=scc_cnt;
if(tmp==i) break;
}
}
}
else low[u]=min(low[u],dfn[v]);
}
if(fa<0&&child==1) iscut[u]=0;
}
void init()
{
tot=0;
scc_cnt=0;
dfs_clock=0;
memset(d,0,sizeof(d));
memset(to,0,sizeof(to));
memset(vis,0,sizeof(vis));
memset(cut,0,sizeof(cut));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(anc,0,sizeof(anc));
memset(info,-1,sizeof(info));
memset(iscut,0,sizeof(iscut));
memset(sccno,0,sizeof(sccno));
while(!s.empty()) s.pop();
for(int i=0; i<=2*n; i++) g[i].clear();
}
void lca_dfs(int x,int dep)
{
vis[x]=1,d[x]=dep;
for(int i=0; i<g[x].size(); i++)
{
int nt=g[x][i];
if(!vis[nt])
{
anc[nt][0]=x;
int k=0;
while(anc[anc[nt][k]][k]!=0)
{
anc[nt][k+1]=anc[anc[nt][k]][k];
k++;
}
lca_dfs(nt,dep+1);
}
}
}
void build()
{
for(int i=1; i<=n; i++)
if(!dfn[i]) tarjan(i,-1);
for(int i=1; i<=n; i++)
if(iscut[i]) cut[i]=++scc_cnt;
for(int i=0; i<tot; i++)
{
int p=to[i];
if(!iscut[p]) continue;
//cout<<"flag "<<i<<" "<<sccno[i]<<" "<<cut[p]<<endl;
g[cut[p]].push_back(sccno[i]);
g[sccno[i]].push_back(cut[p]);
}
memset(vis,0,sizeof(vis));
for(int i=1; i<=scc_cnt; i++)
if(!vis[i]) lca_dfs(i,0);
}
int Lca(int x,int y)
{
if(d[x]<d[y]) swap(x,y);
int l=d[x]-d[y];
int k=0;
while(l!=0)
{
if(l&1) x=anc[x][k];
l>>=1;
k++;
}
k=0;
while(x!=y)
{
if((anc[x][k]!=anc[y][k])||(!k))
{
x=anc[x][k],y=anc[y][k];
k++;
}
else k--;
}
return x;
}
void query()
{
int  q;
scanf("%d",&q);
while(q--)
{
int x,y;
scanf("%d%d",&x,&y);
x--,y--;
x=sccno[2*x];
y=sccno[2*y];
int lca=Lca(x,y);
//cout<<x<<" "<<y<<" "<<lca<<endl;
printf("%d\n",(d[x]+d[y]-2*d[lca])/2);
}
}
int main()
{
#ifdef debug
freopen("in.in","r",stdin);
#endif // debug
while(scanf("%d%d",&n,&m)==2&&(n||m))
{
init();
for(int i=1; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);
}
build();
query();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hdu 双联通分量 LCA