Stability HDU - 5458

Given an undirected connected graph G with n nodes and m edges, with possibly repeated edges and/or loops. The stability of connectedness between node u and node v is defined by the number of edges in this graph which determines the connectedness between them (once we delete this edge, node u and v would be disconnected).

You need to maintain the graph G
, support the deletions of edges (though we guarantee the graph would always be connected), and answer the query of stability for two given nodes.
Input
There are multiple test cases(no more than 3 cases), and the first line contains an integer t, meaning the totally number of test cases.

For each test case, the first line contains three integers n, m and q, where 1≤n≤3×104,1≤m≤105 and 1≤q≤105. The nodes in graph G are labelled from 1 to n.

Each of the following m lines contains two integers u and v describing an undirected edge between node u and node v.

Following q lines - each line describes an operation or a query in the formats:
⋅ 1 a b: delete one edge between a and b. We guarantee the existence of such edge.
⋅ 2 a b: query the stability between a and b
.
Output
For each test case, you should print first the identifier of the test case.

Then for each query, print one line containing the stability between corresponding pair of nodes.

Sample Input

1
10 12 14
1 2
1 3
2 4
2 5
3 6
4 7
4 8
5 8
6 10
7 9
8 9
8 10
2 7 9
2 7 10
2 10 6
2 10 5
1 10 6
2 10 1
2 10 6
2 3 10
1 8 5
2 5 10
2 4 5
1 7 9
2 7 9
2 10 5

Sample Output

Case #1:
0
0
0
0
2
4
3
3
2
3
4

倒着求

#include<bits/stdc++.h>
using namespace std;
const int maxn=500000+100;
struct edge
{
    int to,nxt;
}edge[maxn*2];
int head[maxn],tot;
int top[maxn],fa[maxn],deep[maxn],num[maxn],p[maxn],fp[maxn],son[maxn],pos;
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    pos=0;
    memset(son,-1,sizeof(son));
}
void add_edge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].nxt=head[u];
    head[u]=tot++;
}
void dfs1(int u,int pre,int d)
{
    deep[u]=d;
    fa[u]=pre;
    num[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].nxt)
    {
        int v=edge[i].to;
        if(v!=pre)
        {
            dfs1(v,u,d+1);
            num[u]+=num[v];
            if(son[u]==-1||num[v]>num[son[u]])
                son[u]=v;
        }
    }
}
void getpos(int u,int sp)
{
    top[u]=sp;
    if(son[u]!=-1)
    {
        p[u]=pos++;
        fp[p[u]]=u;
        getpos(son[u],sp);
    }
    else
    {
        p[u]=pos++;
        fp[p[u]]=u;
        return;
    }
    for(int i=head[u];i!=-1;i=edge[i].nxt)
    {
        int v=edge[i].to;
        if(v!=son[u]&&v!=fa[u])
            getpos(v,v);
    }
}
struct Node
{
    int l,r;
    int sum;
    int lazy;
}segtree[maxn*4];
void push_up(int i)
{
    segtree[i].sum=segtree[i<<1].sum+segtree[i<<1|1].sum;
}
void push_down(int i)
{
    if(segtree[i].lazy!=-1)
    {
        segtree[i<<1].sum=segtree[i].lazy;
        segtree[i<<1|1].sum=segtree[i].lazy;
        segtree[i<<1|1].lazy=segtree[i].lazy;
        segtree[i<<1].lazy=segtree[i].lazy;
        segtree[i].lazy=-1;
    }
}
void build(int i,int l,int r)
{
    segtree[i].l=l;
    segtree[i].r=r;
    segtree[i].lazy=-1;
    if(l==r)
    {
        segtree[i].sum=1;
        return;
    }
    int mid=(l+r)/2;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    push_up(i);
}
void update(int i,int l,int r,int val)
{
    if(segtree[i].l==l&&segtree[i].r==r)
    {
        segtree[i].sum=val;
        segtree[i].lazy=val;
        return;
    }
    push_down(i);
    int mid=(segtree[i].l+segtree[i].r)/2;
    if(l>mid)
    {
        update(i<<1|1,l,r,val);
    }
    else if(mid>=r)
    {
        update(i<<1,l,r,val);
    }
    else
    {
        update(i<<1,l,mid,val);
        update(i<<1|1,mid+1,r,val);
    }
    push_up(i);
}
int query(int i,int l,int r)
{
    if(segtree[i].l==l&&segtree[i].r==r)
        return segtree[i].sum;
    push_down(i);
    int mid=(segtree[i].l+segtree[i].r)/2;
    if(r<=mid)
        return query(i<<1,l,r);
    else if(l>mid)
        return query(i<<1|1,l,r);
    else
        return (query(i<<1,l,mid)+query(i<<1|1,mid+1,r));
    push_up(i);
}
int qfindd(int u,int v)
{
    int f1=top[u],f2=top[v];
    int tmp=0;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        tmp=(tmp+query(1,p[f1],p[u]));
        u=fa[f1];
        f1=top[u];
    }
    if(u==v)
        return tmp;
    if(deep[u]>deep[v])
        swap(u,v);
    return (tmp+query(1,p[son[u]],p[v]));
}
void ufindd(int u,int v,int val)
{
    int f1=top[u],f2=top[v];
    int tmp=0;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        update(1,p[f1],p[u],val);
        u=fa[f1];
        f1=top[u];
    }
    if(u==v)
        return;
    if(deep[u]>deep[v])
        swap(u,v);
    update(1,p[son[u]],p[v],val);
}
//bing cha ji
int pre[maxn];
int findd(int x)
{
    int r=x;
    while(r!=pre[r])
        r=pre[r];
    int i=x,j;
    while(i!=r)
    {
        j=pre[i];
        pre[i]=r;
        i=j;
    }
    return r;
}
void bing (int x,int y)
{
    pre[x]=y;
}
//jieshu
typedef pair<int, int> pi;
int op[maxn], x[maxn], y[maxn];
multiset<pi> Map, NewMap;
multiset<pi> :: iterator it;
int ans[maxn];
int main()
{
    int t, kcase = 1;
    scanf("%d",&t);
    while(t--)
    {
        int n, m, q; 
        scanf("%d%d%d",&n,&m,&q);
        init(); 
        Map.clear(); 
        NewMap.clear();
        for(int i = 1; i <= m; i++)
        {
            int u, v;
            scanf("%d%d",&u,&v);
            if(u > v) 
            swap(u, v);
            Map.insert(pi(u, v));
        }
        for(int i = 0; i < q; i++)
        {
            scanf("%d%d%d",&op[i],&x[i],&y[i]);
            if(x[i] > y[i]) 
                swap(x[i], y[i]);
            if(op[i] == 1) 
                Map.erase(Map.find(pi(x[i], y[i])));
        }
        init(); 
        for(int i = 1; i <= n; i++) 
        pre[i] = i;
        for(it = Map.begin(); it != Map.end(); it++)
        {
            int u = findd(it->first), v = findd(it->second);
            if(u == v) continue;
            add_edge(it->first, it->second);
            add_edge(it->second, it->first);
            pre[u] = v; NewMap.insert(*it);
        }

        dfs1(1, 1, 1);  
        getpos(1, 1); 
        build(1, 1, pos);
        for(it = Map.begin(); it != Map.end(); it++)
        {
            if(NewMap.find(*it) == NewMap.end())
            {
                ufindd(it->first, it->second,0);
            }

        }
        printf("Case #%d:\n", kcase++);
        for(int i = q-1; i >= 0; i--)
        {
            if(op[i] == 1) 
                ufindd(x[i], y[i],0);
            else 
                ans[i] = qfindd(x[i], y[i]);
        }
        for(int i = 0; i < q; i++) 
            if(op[i] == 2) 
                printf("%d\n",ans[i]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值