hdoj 5458 Stability 【并查集 + 树链剖分】

本文介绍了一种解决图论问题的方法,通过离线逆序处理的方式,在给定的无向图中支持删除边的操作,并能高效查询两点间的稳定性,即路径上的割边数。

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

Stability

Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 1184    Accepted Submission(s): 259


Problem Description
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 nm and q, where 1n3×104,1m105 and 1q105. 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
 

题意:给定n个点和m条边的无向图,可能存在重边以及自环,有m次操作,1 x y 表示从图中删去x-y的一条边,2 x y 表示查询查询x- y路径上割边的数目。


用LCA倍增也可以搞的,貌似还快一些。

思路:离线,逆序做。首先用并查集建立一棵树,树边权值为1,对于多余的边x y,更新x-y路径上的树边,即边权修改为0(存在环割边不存在)。然后逆序操作,把删边看做加边,每加一条边修改树边权值,至于查询x y就是计算x-y路径上的边权和。


AC代码:卡C++,G++可以过。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (300000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
struct Tree{
    int l, r, sum, lazy, len;
};
Tree tree[MAXN<<2];
void PushUp(int o){
    tree[o].sum = tree[ll].sum + tree[rr].sum;
}
void PushDown(int o)
{
    if(tree[o].lazy != -1)
    {
        tree[ll].lazy = tree[rr].lazy = tree[o].lazy;
        tree[ll].sum = tree[ll].len * tree[o].lazy;
        tree[rr].sum = tree[rr].len * tree[o].lazy;
        tree[o].lazy = -1;
    }
}
void Build(int o, int l, int r)
{
    tree[o].l = l; tree[o].r = r;
    tree[o].sum = tree[o].len = r - l + 1; tree[o].lazy = -1;
    if(l == r)
        return ;
    int mid = (l + r) >> 1;
    Build(lson); Build(rson);
}
void Update(int o, int L, int R, int v)
{
    if(tree[o].l == L && tree[o].r == R)
    {
        tree[o].sum = v * tree[o].len;
        tree[o].lazy = v;
        return ;
    }
    PushDown(o);
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid) Update(ll, L, R, v);
    else if(L > mid) Update(rr, L, R, v);
    else {Update(ll, L, mid, v); Update(rr, mid+1, R, v);}
    PushUp(o);
}
int Query(int o, int L, int R)
{
    if(tree[o].l == L && tree[o].r == R)
        return tree[o].sum;
    PushDown(o);
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid) return Query(ll, L, R);
    else if(L > mid) return Query(rr, L, R);
    else return Query(ll, L, mid) + Query(rr, mid+1, R);
}
struct Edge{
    int from, to, next;
};
Edge edge[MAXN<<1];
int head[MAXN], edgenum;
void init(){
    edgenum = 0; CLR(head, -1);
}
void addEdge(int u, int v)
{
    Edge E = {u, v, head[u]};
    edge[edgenum] = E;
    head[u] = edgenum++;
}
int son[MAXN], num[MAXN];
int top[MAXN], pos[MAXN], id;
int dep[MAXN], pre[MAXN];
void DFS1(int u, int fa, int d)
{
    dep[u] = d; pre[u] = fa; num[u] = 1; son[u] = -1;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == fa) continue;
        DFS1(v, u, d+1);
        num[u] += num[v];
        if(son[u] == -1 || num[son[u]] < num[v])
            son[u] = v;
    }
}
void DFS2(int u, int T)
{
    top[u] = T; pos[u] = ++id;
    if(son[u] == -1) return ;
    DFS2(son[u], T);
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == pre[u] || v == son[u]) continue;
        DFS2(v, v);
    }
}
int GetSum(int u, int v)
{
    int f1 = top[u], f2 = top[v];
    int ans = 0;
    while(f1 != f2)
    {
        if(dep[f1] < dep[f2])
        {
            swap(u, v);
            swap(f1, f2);
        }
        ans += Query(1, pos[f1], pos[u]);
        u = pre[f1], f1 = top[u];
    }
    if(u == v) return ans;
    if(dep[u] > dep[v]) swap(u, v);
    return ans += Query(1, pos[son[u]], pos[v]);
}
void Change(int u, int v)
{
    int f1 = top[u], f2 = top[v];
    while(f1 != f2)
    {
        if(dep[f1] < dep[f2])
        {
            swap(u, v);
            swap(f1, f2);
        }
        Update(1, pos[f1], pos[u], 0);
        u = pre[f1], f1 = top[u];
    }
    if(u == v) return ;
    if(dep[u] > dep[v]) swap(u, v);
    Update(1, pos[son[u]], pos[v], 0);
}
int father[MAXN];
int Find(int p){
    int t, child = p;
    while(p != father[p]) p = father[p];
    while(child != p) {t = father[p]; father[child] = p; child = t;}
    return p;
}
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; Ri(t);
    W(t)
    {
        int n, m, q; Ri(n); Ri(m); Ri(q);
        init(); Map.clear(); NewMap.clear();
        for(int i = 1; i <= m; i++)
        {
            int u, v;
            Ri(u); Ri(v);
            if(u > v) swap(u, v);
            Map.insert(pi(u, v));
        }
        for(int i = 0; i < q; i++)
        {
            Ri(op[i]); Ri(x[i]); Ri(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++) father[i] = i;
        for(it = Map.begin(); it != Map.end(); it++)
        {
            int u = Find(it->fi), v = Find(it->se);
            if(u == v) continue;
            addEdge(it->fi, it->se);
            addEdge(it->se, it->fi);
            father[u] = v; NewMap.insert(*it);
        }

        DFS1(1, -1, 1); id = 0; DFS2(1, 1); Build(1, 1, id);
        for(it = Map.begin(); it != Map.end(); it++)
        {
            if(NewMap.find(*it) == NewMap.end())
                Change(it->fi, it->se);
        }
        printf("Case #%d:\n", kcase++);
        for(int i = q-1; i >= 0; i--)
        {
            if(op[i] == 1) Change(x[i], y[i]);
            else ans[i] = GetSum(x[i], y[i]);
        }
        for(int i = 0; i < q; i++) if(op[i] == 2) Pi(ans[i]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值