1. 程式人生 > >[BZOJ1969][Ahoi2005]LANE 航線規劃

[BZOJ1969][Ahoi2005]LANE 航線規劃

Description

對Samuel星球的探險已經取得了非常巨大的成就,於是科學家們將目光投向了Samuel星球所在的星系——一個巨大的由千百萬星球構成的Samuel星系。 星際空間站的Samuel II巨型計算機經過長期探測,已經鎖定了Samuel星系中許多星球的空間座標,並對這些星球從1開始編號1、2、3……。 一些先遣飛船已經出發,在星球之間開闢探險航線。 探險航線是雙向的,例如從1號星球到3號星球開闢探險航線,那麼從3號星球到1號星球也可以使用這條航線。 例如下圖所示: 在5個星球之間,有5條探險航線。 A、B兩星球之間,如果某條航線不存在,就無法從A星球抵達B星球,我們則稱這條航線為關鍵航線。 顯然上圖中,1號與5號星球之間的關鍵航線有1條:即為4-5航線。 然而,在宇宙中一些未知的磁暴和行星的衝撞,使得已有的某些航線被破壞,隨著越來越多的航線被破壞,探險飛船又不能及時回覆這些航線,可見兩個星球之間的關鍵航線會越來越多。 假設在上圖中,航線4-2(從4號星球到2號星球)被破壞。此時,1號與5號星球之間的關鍵航線就有3條:1-3,3-4,4-5。 小聯的任務是,不斷關注航線被破壞的情況,並隨時給出兩個星球之間的關鍵航線數目。現在請你幫助完成。

Input

第一行有兩個整數N,M。表示有N個星球(1< N < 30000),初始時已經有M條航線(1 < M < 100000)。隨後有M行,每行有兩個不相同的整數A、B表示在星球A與B之間存在一條航線。接下來每行有三個整數C、A、B。C為1表示詢問當前星球A和星球B之間有多少條關鍵航線;C為0表示在星球A和星球B之間的航線被破壞,當後面再遇到C為1的情況時,表示詢問航線被破壞後,關鍵路徑的情況,且航線破壞後不可恢復; C為-1表示輸入檔案結束,這時該行沒有A,B的值。被破壞的航線數目與詢問的次數總和不超過40000。

Output

對每個C為1的詢問,輸出一行一個整數表示關鍵航線數目。 注意:我們保證無論航線如何被破壞,任意時刻任意兩個星球都能夠相互到達。在整個資料中,任意兩個星球之間最多隻可能存在一條直接的航線。

Sample Input

5 5
1 2
1 3
3 4
4 5
4 2
1 1 5
0 4 2
1 5 1
-1

Sample Output

1
3

 


 

 

只有撤銷操作沒有加入操作,這很容易想到要離線逆序處理,我們發現題目中的一句話十分要命:“注意:我們保證無論航線如何被破壞,任意時刻任意兩個星球都能夠相互到達。在整個資料中,任意兩個星球之間最多隻可能存在一條直接的航線。”。這個圖任意時刻都聯通,它至少是一棵樹,所以我們先對刪完邊的圖求一個生成樹,樹上的邊權都是1,因為每條邊都是關鍵路徑,然後按照詢問逆序處理,如果是破壞邊,就把(x, y)的區間的邊賦為0,表示這些邊不可能成為關鍵邊,如果是詢問直接查詢(x, y)的樹上的路徑的1的個數。

注意在建成生成樹之後要把非樹邊先計算上。

以上操作用樹鏈剖分做就特別簡單了。

並查集記得路徑壓縮...忘了寫路徑壓縮T到懷疑人生。

程式碼不長。

 


 

 

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
using namespace std;
#define reg register
inline int read() {
    int res = 0;char ch=getchar();bool fu=0;
    while(!isdigit(ch))fu|=(ch=='-'),ch=getchar();
    while(isdigit(ch))res=(res<<3)+(res<<1)+(ch^48), ch=getchar();
    return fu?-res:res;
}
#define N 30005
#define M 100005
#define pii pair<int, int> 
#define mkp make_pair

int n, m, q;
struct que {
    int opt, x, y;
}qu[N*2];
struct EDGE {
    int x, y;
}EDG[M];
struct edge {
    int nxt, to;
}ed[N<<1];
int head[N], cnt;
inline void add(int x, int y) {
    ed[++cnt] = (edge){head[x], y};
    head[x] = cnt;
}
int ufs[N];
int getF(int x) {return x==ufs[x]?x:ufs[x]=getF(ufs[x]);}
map <pii, int> ex, use;

int siz[N], father[N], son[N], dep[N], id[N], rnk[N], top[N], tot;
void dfs1(int x, int fa)
{
    siz[x] = 1;
    dep[x] = dep[fa] + 1;
    father[x] = fa;
    for (reg int i = head[x] ; i ; i = ed[i].nxt)
    {
        int to = ed[i].to;
        if (to == fa) continue;
        dfs1(to, x);
        siz[x] += siz[to];
        if (siz[to] > siz[son[x]]) son[x] = to;
    }
}
void dfs2(int x, int tep)
{
    id[x] = ++tot, rnk[tot] = x, top[x] = tep;
    if (son[x]) dfs2(son[x], tep);
    for (reg int i = head[x] ; i ; i = ed[i].nxt)
    {
        int to = ed[i].to;
        if (to == son[x] or to == father[x]) continue;
        dfs2(to, to);
    }
}

int tr[N<<2], tag[N<<2];
#define ls o << 1
#define rs o << 1 | 1
void build(int l, int r, int o)
{
    if (l == r) {tr[o]=1;return;}
    int mid=(l+r)>>1;
    build(l, mid, ls), build(mid+1,r, rs);
    tr[o]=tr[ls]+tr[rs];
}
inline void spread(int o)
{
    if (!tag[o]) return ;
    tag[ls] = tag[rs] = 1;
    tag[o] = 0;
    tr[ls] = tr[rs] = 0;
}
void change(int l, int r, int o, int ql, int qr)
{
    if (l >= ql and r <= qr) {
        tr[o] = 0, tag[o] = 1;
        return ;
    }
    int mid=(l+r)>>1;
    spread(o);
    if (ql <= mid) change(l, mid, ls, ql, qr);
    if (qr > mid) change(mid + 1, r, rs, ql, qr);
    tr[o] = tr[ls] + tr[rs];
}
int query(int l, int r, int o, int ql, int qr)
{
    if (l >= ql and r <= qr) return tr[o];
    spread(o);
    int mid=(l+r)>>1, res=0;
    if (ql<=mid) res+=query(l, mid, ls, ql, qr);
    if (qr>mid) res+=query(mid+1, r, rs, ql, qr);
    return res;
}
inline void changes(int x, int y)
{
    while(top[x] != top[y]) 
    {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        change(1, n, 1, id[top[x]], id[x]);
        x = father[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    change(1, n, 1, id[x] + 1, id[y]);
}
inline int querys(int x, int y) 
{
    int res = 0;
    while(top[x]!=top[y])
    {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        res += query(1, n, 1, id[top[x]], id[x]);
        x = father[top[x]];
    }
    if (id[x] > id[y]) swap(x, y);
    res += query(1, n, 1, id[x] + 1, id[y]);
    return res;
}
int ans[N*2], ttans;
int main()
{
    n = read(), m = read();
    for (reg int i = 1 ; i <= m ; i ++) {
        EDG[i] = (EDGE){read(), read()};
        if (EDG[i].x > EDG[i].y) swap(EDG[i].x, EDG[i].y);
        ex[mkp(EDG[i].x, EDG[i].y)] = 1;
    }
    while(1)
    {
        int opt = read();
        if (opt == -1) break;
        qu[++q] = (que){opt, read(), read()};
        if (qu[q].x > qu[q].y) swap(qu[q].x, qu[q].y);
        if (opt == 0) ex[mkp(qu[q].x, qu[q].y)] = 0;
    }
    for (reg int i = 1 ; i <= n ; i ++) ufs[i] = i;
    for (reg int i = 1 ; i <= m ; i ++)
    {
        int x = EDG[i].x, y = EDG[i].y;
        if (!ex[mkp(x, y)]) continue;
        int fx = getF(x), fy = getF(y);
        if (fx == fy) continue;
        ufs[fx] = fy;
        add(x, y), add(y, x);
        use[mkp(x, y)] = 1;
    }
    dfs1(1, 0), dfs2(1, 1);
    build(1, n, 1);
    for (reg int i = 1 ; i <= m ; i ++)
    {
        int x = EDG[i].x, y = EDG[i].y;
        if (!ex[mkp(x, y)]) continue;
        if (!use[mkp(x, y)]) changes(x, y);
    }
    for (reg int i = q ; i >= 1 ; i --)
    {
        int op = qu[i].opt, x = qu[i].x, y = qu[i].y;
        if (!op) changes(x, y);
        else ans[++ttans] = querys(x, y);
    }
    for (reg int i = ttans ; i >= 1 ; i --) printf("%d\n", ans[i]);
    return 0;
}