One-Way Conveyors 【Tarjan縮點+樹鏈剖分】

One-Way Conveyors

 Aizu - 1408


  這道題上面推薦大家把maxN開的大一點,不然會RE,我倒是被這個坑了一下,還有一些細節,我將在後面徐徐道來。(目前最快還是比較的開心)

  先講一下題意:有N個點,M條無向邊,然後輸入這M條無向邊,再是一個K,表示我們有K個要求,每個u到v,我們現在要把原來的無向圖變成有向圖,也就是給每條邊定向,還要滿足所有K個u到v都要滿足從u能抵達v。如果衝突就輸出No,不然就是Yes加上每條邊的方向,SPJ問題。

  接下去,講一下我的想法,當然也是題解了,這個問題比較的繁瑣,我們將它拆開來考慮。

環內

  環內的所有點都是滿足兩兩之間可以任意的相互到達,所以環內的情況,環內的邊是可以隨便跑的。

環與環之間

  除了環,就是環與環之間的邊了,如果說我們可以要從環A的u點到達環B的v點,那麼說明環A到環B的邊是由環A到環B的,我們可以確定任意兩環的通道是唯一的,不然他們就在一個環內。所以,我們可以給環A到環B定向,因爲他們的路徑上有可能出現別的縮點的環,所以這些路徑也會被定向。

如何解決環與環之間的割邊的定向問題

  這裏就可以用縮點之後的圖來解決,不難發現,縮點之後的圖是多棵有根樹,我利用超級原點0來將他們都鏈接起來。所以,就變成了一棵樹上的問題,現在我們要是的u到v定向,實際上就是u所屬的環向v所屬的環定向了,於是,我們可以利用樹鏈剖分來解決這個問題,於是完成了這些割邊的定向。

  如果兩點本身不相互鏈接,那麼還是要輸出No,這裏需要單獨判斷一下。

Yes時候輸出的方法

  對於環內的,我們直接跑就可以了,對於環外的,也就是環與環鏈接的時候,我們可以利用樹鏈剖分上維護的信息來確定,這裏自己模擬一下很快就知道了。因爲我們樹鏈剖分操作的點也本身是環點,環與環鏈接的邊又是可以唯一確定的,所以這個問題也就引刃而解了。

代碼有點長,有些小細節上面要是沒有提到的話,可以在評論區call me!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 1e5 + 7, maxM = 1e6 + 7;
int N, M, Q, root[maxN];
inline int fid(int x) { return x == root[x] ? x : root[x] = fid(root[x]); }
struct Graph
{
    int head[maxN], cnt;
    struct Eddge
    {
        int nex, to;
        Eddge(int a=-1, int b=0):nex(a), to(b) {}
    } edge[maxM << 1];
    inline void addEddge(int u, int v)
    {
        edge[cnt] = Eddge(head[u], v);
        head[u] = cnt++;
    }
    inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
    inline void init()
    {
        cnt = 0;
        for(int i=0; i<=N; i++) head[i] = -1;
    }
} Old, Now;
bool vis[maxM << 1] = {false}, instack[maxN] = {false};
int dfn[maxN], tot, low[maxN], Stap[maxN], Stop, Belong[maxN], Bcnt;
void Tarjan(int u)
{
    dfn[u] = low[u] = ++tot;
    Stap[++Stop] = u;
    instack[u] = true;
    for(int i=Old.head[u], v; ~i; i=Old.edge[i].nex)
    {
        if(vis[i]) continue;
        vis[i] = vis[i ^ 1] = true;
        v = Old.edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(instack[v]) low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u])
    {
        int v; Bcnt++;
        do
        {
            v = Stap[Stop--];
            instack[v] =  false;
            Belong[v] = Bcnt;
        } while(v ^ u);
    }
}
bool read[maxM << 1] = {false};
inline void Build_New_Graph()
{
    for(int u=1; u<=N; u++)
    {
        for(int i=Old.head[u], v; ~i; i=Old.edge[i].nex)
        {
            if(read[i]) continue;
            read[i] = read[i ^ 1] = true;
            v = Old.edge[i].to;
            if(Belong[u] == Belong[v]) continue;
            Now._add(Belong[u], Belong[v]);
        }
    }
}
int siz[maxN], Wson[maxN], deep[maxN], fa[maxN];
void dfs_1(int u, int father)
{
    siz[u] = 1; deep[u] = deep[father] + 1; fa[u] = father;
    int maxx = 0;
    for(int i=Now.head[u], v; ~i; i=Now.edge[i].nex)
    {
        v = Now.edge[i].to;
        if(v == father) continue;
        dfs_1(v, u);
        siz[u] += siz[v];
        if(maxx < siz[v])
        {
            maxx = siz[v];
            Wson[u] = v;
        }
    }
}
int top[maxN], id[maxN], _Index;
void dfs_2(int u, int topy)
{
    top[u] = topy; id[u] = ++_Index;
    if(Wson[u]) dfs_2(Wson[u], topy);
    for(int i=Now.head[u], v; ~i; i=Now.edge[i].nex)
    {
        v = Now.edge[i].to;
        if(v == fa[u] || v == Wson[u]) continue;
        dfs_2(v, v);
    }
}
bool ok;
struct BIT_Tree
{
    int tree[maxN << 1] = {0}, lazy[maxN << 1] = {0};   //1 == down and 2 == up
    inline void pushdown(int rt)
    {
        if(lazy[rt])
        {
            tree[lsn] = lazy[rt]; tree[rsn] = lazy[rt];
            lazy[lsn] = lazy[rt]; lazy[rsn] = lazy[rt];
            lazy[rt]  = 0;
        }
    }
    inline void pushup(int rt) { tree[rt] = tree[lsn] | tree[rsn]; }
    void update(int rt, int l, int r, int ql, int qr, int state)    //state = 0(down) or 1(up)
    {
        if(ql <= l && qr >= r)
        {
            if(tree[rt] & (1 << (1 - state))) ok = false;
            tree[rt] = 1 << state; lazy[rt] = 1 << state;
            return;
        }
        int mid = HalF; pushdown(rt);
        if(qr <= mid) update(QL, state);
        else if(ql > mid) update(QR, state);
        else { update(QL, state); update(QR, state); }
        pushup(rt);
    }
    int query(int rt, int l, int r, int qx)
    {
        if(l == r) return tree[rt];
        int mid = HalF; pushdown(rt);
        if(qx <= mid) return query(Lson, qx);
        else return query(Rson, qx);
    }
    inline void Q_Range(int u, int v)   //from u to v
    {
        int op = 1;
        while(top[u] ^ top[v])
        {
            if(deep[top[u]] < deep[top[v]]) { op ^= 1; swap(u, v); }
            update(1, 1, _Index, id[top[u]], id[u], op);
            u = fa[top[u]];
        }
        if(deep[u] > deep[v])
        {
            update(1, 1, _Index, id[Wson[v]], id[u], op);
        }
        else if(deep[u] < deep[v])
        {
            update(1, 1, _Index, id[Wson[u]], id[v], op ^ 1);
        }
    }
} tree;
bool sured[maxM << 1] = {false}, seen[maxN] = {false}, Bas_point[maxN] = {false};
void OutPut_dfs(int u)
{
    for(int i=Old.head[u], v, bel_u, bel_v, op; ~i; i=Old.edge[i].nex)
    {
        if(sured[i]) continue;
        sured[i] = sured[i ^ 1] = true;
        v = Old.edge[i].to;
        if(Belong[u] ^ Belong[v])
        {
            bel_u = Belong[u]; bel_v = Belong[v];
            if(deep[bel_u] > deep[bel_v])
            {
                op = tree.query(1, 1, _Index, id[bel_u]);
                if(op == 1) printf("%d %d\n", v, u);
                else printf("%d %d\n", u, v);
            }
            else
            {
                op = tree.query(1, 1, _Index, id[bel_v]);
                if(op == 1) printf("%d %d\n", u, v);
                else printf("%d %d\n", v, u);
            }
        }
        else
        {
            printf("%d %d\n", u, v);
            if(Bas_point[v]) continue;
            Bas_point[v] = true;
            OutPut_dfs(v);
        }
    }
}
inline void init()
{
    tot = Stop = Bcnt = _Index = 0;
    Old.init(); Now.init(); ok = true;
    for(int i=1; i<=N; i++) root[i] = i;
}
int main()
{
    scanf("%d%d", &N, &M);
    init();
    for(int i=1, u, v, fu, fv; i<=M; i++)
    {
        scanf("%d%d", &u, &v);
        Old._add(u, v); fu = fid(u); fv = fid(v);
        if(fu ^ fv)
        {
            root[fu] = fv;
        }
    }
    for(int i=1; i<=N; i++) if(!dfn[i]) { Tarjan(i); Now._add(0, Belong[i]); }
    Build_New_Graph();
    dfs_1(0, 0);
    dfs_2(0, 0);
    scanf("%d", &Q);
    for(int i=1, x, y; i<=Q; i++)
    {
        scanf("%d%d", &x, &y);
        if(fid(x) ^ fid(y)) ok = false;
        if(!ok) continue;
        if(Belong[x] == Belong[y]) continue;
        tree.Q_Range(Belong[x], Belong[y]);
    }
    if(!ok) { printf("No\n"); return 0; }
    printf("Yes\n");
    for(int i=1; i<=N; i++)
    {
        if(seen[Belong[i]]) continue;
        seen[Belong[i]] = true;
        Bas_point[i] = true;
        OutPut_dfs(i);
    }
    return 0;
}

 

發佈了799 篇原創文章 · 獲贊 960 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章