poj3694 Network

本文探讨了网络中桥接的重要性及其对数据传输的影响,并提出了一种通过不断添加新链接来消除所有桥接的算法解决方案。该方案首先使用Tarjan算法进行节点压缩,保留关键桥接形成树状结构,再利用树链剖分技术进行维护。

Description

A network administrator manages a large network. The network consists
of N computers and M links between pairs of computers. Any pair of
computers are connected directly or indirectly by successive links, so
data can be transformed between any two computers. The administrator
finds that some links are vital to the network, because failure of any
one of them can cause that data can’t be transformed between some
computers. He call such a link a bridge. He is planning to add some
new links one by one to eliminate all bridges.

You are to help the administrator by reporting the number of bridges
in the network after each new link is added.

Input

The input consists of multiple test cases. Each test case starts with
a line containing two integers N(1 ≤ N ≤ 100,000) and M(N - 1 ≤ M ≤
200,000). Each of the following M lines contains two integers A and B
( 1≤ A ≠ B ≤ N), which indicates a link between computer A and B.
Computers are numbered from 1 to N. It is guaranteed that any two
computers are connected in the initial network. The next line contains
a single integer Q ( 1 ≤ Q ≤ 1,000), which is the number of new links
the administrator plans to add to the network one by one. The i-th
line of the following Q lines contains two integer A and B (1 ≤ A ≠ B
≤ N), which is the i-th added new link connecting computer A and B.

The last test case is followed by a line containing two zeros.

Output

For each test case, print a line containing the test case number(
beginning with 1) and Q lines, the i-th of which contains a integer
indicating the number of bridges in the network after the first i new
links are added. Print a blank line after the output for each test
case.

先用tarjan缩点只保留桥成一棵树,然后每增加一条边相当于使路径上的边都变得不是桥,所以用树链剖分维护。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
#define M(a) memset(a,0,sizeof(a))
#define LL long long
const int p1=131,p2=13331,m1=1000007,m2=2333333;
int f1[100010],n1[400010],t1[400010],
fir[100010],ne[400010],to[400010],
sta[100010],dfn[100010],low[100010],bel[100010],
fa[100010],son[100010],dep[100010],size[100010],
pos[100010],top[100010],tag[400010],sum[400010],
n,m,num,clo,stp,tot;
bool v1[1000010],v2[3000010];
void add1(int num,int u,int v)
{
    n1[num]=f1[u];
    f1[u]=num;
    t1[num]=v;
}
void add2(int num,int u,int v)
{
    ne[num]=fir[u];
    fir[u]=num;
    to[num]=v;
}
bool init()
{
    clo=num=stp=tot=0;
    int i,x,y;
    scanf("%d%d",&n,&m);
    if (!n) return 0;
    for (i=1;i<=n;i++)
      f1[i]=dfn[i]=0;
    for (i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        add1(i*2,x,y);
        add1(i*2+1,y,x);
    }
    return 1;
}
void dfs1(int u,int fa)
{
    int i,v,x;
    sta[++stp]=u;
    dfn[u]=low[u]=++clo;
    for (i=f1[u];i;i=n1[i])
      if (i/2!=fa)
      {
        v=t1[i];
        if (!dfn[v])
        {
            dfs1(v,i/2);
            low[u]=min(low[u],low[v]);
        }
        else low[u]=min(low[u],dfn[v]);
      }
    if (low[u]==dfn[u])
    {
        num++;
        do
        {
            x=sta[stp--];
            bel[x]=num;
        }
        while (x!=u);
    }
}
void make()
{
    int i,u,v,x,y,h1,h2,h3,h4,cnt=0;
    dfs1(1,-1);
    for (i=1;i<=num;i++)
      fir[i]=0;
    M(v1);
    M(v2);
    for (u=1;u<=n;u++)
      for (i=f1[u];i;i=n1[i])
        if ((x=bel[u])!=(y=bel[v=t1[i]]))
        {
            h1=((LL)x*p1%m1+(LL)y*p2%m1)%m1;
            h2=((LL)y*p1%m1+(LL)x*p2%m1)%m1;
            h3=((LL)x*p1%m2+(LL)y*p2%m2)%m2;
            h4=((LL)y*p1%m2+(LL)x*p2%m2)%m2;
            if (v1[h1]&&v1[h2]&&v2[h3]&&v2[h4]) continue;
            cnt++;
            add2(cnt*2,x,y);
            add2(cnt*2+1,y,x);
            v1[h1]=v1[h2]=v2[h3]=v2[h4]=1;
        }
}
void down(int p,int L,int R)
{
    if (tag[p])
    {
        tag[p*2]=tag[p*2+1]=1;
        sum[p]=0;
        tag[p]=0;
    }
}
void up(int p,int L,int R)
{
    int mid=(L+R)/2;
    down(p,L,R);
    down(p*2,L,mid);
    down(p*2+1,mid+1,R);
    sum[p]=sum[p*2]+sum[p*2+1];
}
int modi(int p,int L,int R,int l,int r)
{
    down(p,L,R);
    if (R==r&&L==l)
    {
        tag[p]=1;
        return sum[p];
    }
    int mid=(L+R)/2,ret;
    if (r<=mid) ret=modi(p*2,L,mid,l,r);
    else
    {
        if (l>=mid+1) ret=modi(p*2+1,mid+1,R,l,r);
        else ret=modi(p*2,L,mid,l,mid)+modi(p*2+1,mid+1,R,mid+1,r);
    }
    up(p,L,R);
    return ret;
}
void add(int p,int L,int R)
{
    tag[p]=0;
    if (L==R)
    {
        sum[p]=!(L==1);
        return;
    }
    int mid=(L+R)/2;
    add(p*2,L,mid);
    add(p*2+1,mid+1,R);
    sum[p]=sum[p*2]+sum[p*2+1];
}
void dfs2(int u)
{
    int i,v;
    size[u]=1;
    son[u]=0;
    for (i=fir[u];i;i=ne[i])
      if ((v=to[i])!=fa[u])
      {
        dep[v]=dep[u]+1;
        fa[v]=u;
        dfs2(v);
        size[u]+=size[v];
        if (!son[u]||size[v]>size[son[u]])
          son[u]=v;
      }
}
void dfs3(int u)
{
    int i,v;
    pos[u]=++tot;
    if (!son[u]) return;
    top[son[u]]=top[u];
    dfs3(son[u]);
    for (i=fir[u];i;i=ne[i])
      if ((v=to[i])!=fa[u]&&v!=son[u])
      {
        top[v]=v;
        dfs3(v);
      }
}
void build()
{
    int i;
    dep[1]=1;
    dfs2(1);
    top[1]=1;
    dfs3(1);
    add(1,1,tot);
}
void solve()
{
    int q,u,v,ans=num-1;
    scanf("%d",&q);
    while (q--)
    {
        scanf("%d%d",&u,&v);
        u=bel[u];
        v=bel[v];
        while (top[u]!=top[v])
        {
            if (dep[top[u]]<dep[top[v]]) swap(u,v);
            ans-=modi(1,1,tot,pos[top[u]],pos[u]);
            u=fa[top[u]];
        }
        if (dep[u]<dep[v]) swap(u,v);
        if (u!=v) ans-=modi(1,1,tot,pos[son[v]],pos[u]);
        printf("%d\n",ans);
    }
}
int main()
{
    int K=0;
    while (init())
    {
        printf("Case %d:\n",++K);
        make();
        build();
        solve();
        putchar('\n');
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值