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

本文介绍了一种解决特定图论问题的方法,即通过计算双连通分量和割点来确定任意两条边间必经顶点的数量。具体而言,首先通过Tarjan算法找出所有双连通分量并进行缩点处理,然后构造一棵树来表示这些分量之间的连接关系。最后利用LCA算法计算路径上的割点数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值