【POJ3694】Network {tarjan+并查集}

该博客讲述了如何利用tarjan算法和并查集解决一个网络分割问题。具体情境是SN公司面临DN公司的攻击,原始网络被视为一棵树,经理Yixght增加了M条新通道。作为黑客,目标是破坏两条通道使网络分割成至少两部分。博客给出了输入输出格式,并举例说明。博主解析了使用tarjan缩点得到无环图,以及在新边连接后如何判断割边变化的方法,特别强调了当两点不在同一连通块时,新形成的环会影响割边数量。最后,博主分享了在实现过程中的思考误区和注意事项,如数组大小的设定等。
摘要由CSDN通过智能技术生成
  • 【Description】
    Yixght is a manager of the company called SzqNetwork(SN). Now she’s very worried because she has just received a bad news which denotes that DxtNetwork(DN), the SN’s business rival, intents to attack the network of SN. More unfortunately, the original network of SN is so weak that we can just treat it as a tree. Formally, there are N nodes in SN’s network, N-1 bidirectional channels to connect the nodes, and there always exists a route from any node to another. In order to protect the network from the attack, Yixght builds M new bidirectional channels between some of the nodes.
    As the DN’s best hacker, you can exactly destory two channels, one in the original network and the other among the M new channels. Now your higher-up wants to know how many ways you can divide the network of SN into at least two parts.

  • 【Input】
    The first line of the input file contains two integers: N (1 ≤ N ≤ 100 000), M (1 ≤ M ≤ 100 000) — the number of the nodes and the number of the new channels.
    Following N-1 lines represent the channels in the original network of SN, each pair (a,b) denote that there is a channel between node a and node b.
    Following M lines represent the new channels in the network, each pair (a,b) denote that a new channel between node a and node b is added to the network of SN.

  • 【Output】
    Output a single integer — the number of ways to divide the network into at least two parts.

  • 【Sample Input】
    4 1
    1 2
    2 3
    1 4
    3 4

  • 【Sample Output】
    3


【题目大意】给出一张n个点、m条边的无向图,现有Q个询问,每个询问给出一条边 (u,v),求加入这条边后图中桥(割边)的数量。

【题解】tarjan+并查集
虽然题目给出了一张无向图,但由于要求的是割边数量,环显然是无用的。所以先进行tarjan缩点,得到一棵树,树上的边即为初始的割边。

对于询问给出的边 (u,v),有两种情况:
<1>若u,v两点在一个连通块内,此边对割边数量没有影响;
<2>若u,v两点不在一个连通块内,则加入这条边之后会形成一个新的环,这个环上的其他边就是减少的割边。

考虑情况<2>,在形成新环后,我们从u,v两点向上走。一直走到两点的公共祖先后停止,经过的路径就是环上的边。由于整棵树的边数小于n,暴力地扫描这些边就可以了。扫描完后,将路径上的点合并。合并时用到并查集。
//详见程序。


#include <cstdio>
#include <iostream>
#include <cstring> 
#define N 100005
struct edge{ int to,nxt;}e[N<<2],e1[N<<2];
int n,m,Q,col,clo,t,cnt,cnt1,
    fi[N],fi1[N],dfn[N],low[N],z[N],c[N],q[N],fa[N],d[N],f[N];
    void add(int u,int v)
    {
        e[++cnt].to=v;e[cnt].nxt=fi[u];fi[u]=cnt;
    }
    void add1(int u,int v)
    {
        e1[++cnt1].to=v;e1[cnt1].nxt=fi1[u];fi1[u]=cnt1;
    }
    void tarjan(int x,int k)
    {
        dfn[x]=low[x]=++clo;z[++t]=x;
        for (int i=fi1[x];i;i=e1[i].nxt)
            if (!dfn[e1[i].to])
            {
                tarjan(e1[i].to,i^1);
                low[x]=std::min(low[x],low[e1[i].to]); 
            }
            else if (i!=k) low[x]=std::min(low[x],dfn[e1[i].to]);
        if (dfn[x]==low[x])
        {
            for (++col;z[t]!=x;c[z[t--]]=col);
            c[z[t--]]=col;
        } 
    }

    int find(int x){ return f[x]==x?x:f[x]=find(f[x]);}
    int dfs(int x,int ff)
    {
        for (int i=fi[x];i;i=e[i].nxt)
            if (e[i].to!=ff)
            {
                d[e[i].to]=d[x]+1;
                fa[e[i].to]=x;
                dfs(e[i].to,x);
            }
    }
int main()
{
    for (int cas=1;~scanf("%d%d\n",&n,&m);++cas)
    {
        if (!n && !m) break;
        printf("Case %d:\n",cas);
        cnt1=1;cnt=clo=t=col=0;
        memset(fi,0,sizeof(fi));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(f,0,sizeof(f));
        memset(d,0,sizeof(d));
        memset(c,0,sizeof(c));
        memset(fi1,0,sizeof(fi1));
        for (int i=1;i<=m;++i)
        {
            int u,v;scanf("%d%d\n",&u,&v);
            add1(u,v);add1(v,u);
        }
        for (int i=1;i<=n;++i)
            if (!dfn[i]) tarjan(i,0);
        for (int i=1;i<=n;++i)
            for (int j=fi1[i];j;j=e1[j].nxt)
                if (c[i]!=c[e1[j].to]) add(c[i],c[e1[j].to]);
        for (int i=1;i<=col;++i) f[i]=i;
        --col;dfs(1,0);scanf("%d\n",&Q);
        for (;Q--;)
        {
            int u,v;scanf("%d%d\n",&u,&v);
            u=find(c[u]);v=find(c[v]);
            if (d[u]<d[v]) std::swap(u,v);
            int i=u,j=v;
            for (;f[i]!=f[j];)
            {
                if (d[f[i]]<d[f[j]]) std::swap(i,j);
                i=find(fa[i]);--col;
            }
            printf("%d\n",col);
            if (u==v) continue;
            for (;f[u]!=f[v];)
            {
                if (d[f[u]]<d[f[v]]) std::swap(u,v);
                f[u]=i;u=find(fa[u]);
            }
        }
        printf("\n");
    }
    return 0;
}

【题外话】做题的时候绕了好几个弯路,感觉最近总是很容易想偏题= =心累累的
最后数组开小了在POJ上RE了无数次(因为树的题做多了意识里感觉这题也是树就开了n*2的数组TAT)

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值