1. 程式人生 > >UOJ 30 【CF Round #278】Tourists

UOJ 30 【CF Round #278】Tourists

Tourists

UOJ 30

1

題目大意

\(n\)\(m\) 條邊的無向圖,每個點有點權 \(w_i\)\(q\) 次詢問,每次修改第 \(a\) 個點的點權為 \(w\) ,或者查詢 \(a\)\(b\) 所有路徑中,最小的點權

資料範圍

\(1 \le n,m,q \le 10^5, 1 \le w_i \le 10^9\)

時空限制

2s,256MB

分析

對於這種在無向圖上查詢路徑資訊的題,一般利用圓方樹將其轉為樹上問題

對於一個方點,維護點雙中最小的點權,但是如果這樣,每修改一次就需要修改所有與它相鄰的節點。

對此我們有一個經典方法,對於方點維護除了環頂之外的資訊,在圓方樹中就相當於方點維護所有它兒子的資訊,那麼我們修改時只需要修改它的父親,查詢的時候如果 \(lca\)

為方點,那麼還需要加上它的父親的資訊(因為父親也在此點雙中)

Code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <set>

using namespace std;

template<class T> void read(T & x)
{
    x = 0; int f = 1, ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 - '0' + ch;
        ch = getchar();
    }
    x *= f;
}

#define lson u << 1, l, mid
#define rson u << 1 | 1, mid + 1, r

const int inf = 1000000000;

const int maxn = 100000 + 5;
const int maxm = 100000 + 5;
const int maxe = maxm * 2;
const int maxnode = maxn * 2;

int n, m, q;
int val[maxnode];

struct edge
{
    int to, nex;
    edge(int to = 0, int nex = 0) : to(to), nex(nex) {}
} g[maxe];
int head[maxn];
int ecnt;
vector<int> adj[maxnode];

inline void addedge(int u, int v)
{
    g[ecnt] = edge(v, head[u]), head[u] = ecnt++;
    g[ecnt] = edge(u, head[v]), head[v] = ecnt++;
}
inline void adde(int u, int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}

int dfc, dfn[maxn], low[maxn];
int top, sta[maxn];
int bcnt;
multiset<int> bccval[maxn];

void tarjan(int u, int fa)
{
    dfn[u] = low[u] = ++dfc;
    sta[++top] = u;
    for(int i = head[u]; ~ i; i = g[i].nex)
    {
        int v = g[i].to;
        if(v != fa)
        {
            if(!dfn[v])
            {
                tarjan(v, u);
                low[u] = min(low[u], low[v]);
                if(low[v] >= dfn[u])
                {
                    int now = n + (++bcnt);
                    adde(now, u);
                    while(true)
                    {
                        int x = sta[top--];
                        bccval[bcnt].insert(val[x]);
                        adde(now, x);
                        if(x == v) break;
                    }
                    val[now] = * bccval[bcnt].begin();
                }
            }
            else low[u] = min(low[u], dfn[v]);
        }
    }
}

struct segment_tree
{
    int mn[maxnode << 2];

    inline void pushup(int u)
    {
        mn[u] = min(mn[u << 1], mn[u << 1 | 1]);
    }
    void build(int u, int l, int r)
    {
        if(l == r)
        {
            mn[u] = val[ver[l]];
            return;
        }
        int mid = (l + r) >> 1;
        build(lson), build(rson);
        pushup(u);
    }
    void update(int u, int l, int r, int qp)
    {
        if(l == r)
        {
            mn[u] = val[ver[l]];
            return;
        }
        int mid = (l + r) >> 1;
        if(qp <= mid) update(lson, qp);
        else update(rson, qp);
        pushup(u);
    }
    int query(int u, int l, int r, int ql, int qr)
    {
        if(l == ql && r == qr) return mn[u];
        int mid = (l + r) >> 1;
        if(qr <= mid) return query(lson, ql, qr);
        else if(ql > mid) return query(rson, ql, qr);
        else
        {
            int lv = query(lson, ql, mid);
            int rv = query(rson, mid + 1, qr);
            return min(lv, rv);
        }
    }

    int dep[maxnode], anc[maxnode], siz[maxnode], son[maxnode];
    int dfn[maxnode], top[maxnode], ver[maxnode], dfc;

    void dfs1(int u)
    {
        siz[u] = 1;
        for(unsigned int i = 0; i < adj[u].size(); ++i)
        {
            int v = adj[u][i];
            if(v != anc[u])
            {
                dep[v] = dep[u] + 1;
                anc[v] = u;
                dfs1(v);
                siz[u] += siz[v];
                if(siz[son[u]] <= siz[v]) son[u] = v;
            }
        }
    }
    void dfs2(int u, int chain)
    {
        top[u] = chain;
        dfn[u] = ++dfc;
        ver[dfc] = u;
        if(son[u])
        {
            dfs2(son[u], chain);
        }
        for(unsigned int i = 0; i < adj[u].size(); ++i)
        {
            int v = adj[u][i];
            if(v != anc[u] && v != son[u])
            {
                dfs2(v, v);
            }
        }
    }
    void init()
    {
        dfs1(1);
        dfs2(1, 1);
        build(1, 1, n + bcnt);
    }

    void update(int u, int x)
    {
        int v = anc[u];
        if(v) bccval[v - n].erase(bccval[v - n].find(val[u]));
        val[u] = x;
        if(v) bccval[v - n].insert(val[u]);
        if(v) val[v] = * bccval[v - n].begin();
        update(1, 1, n + bcnt, dfn[u]);
        if(v) update(1, 1, n + bcnt, dfn[v]);
    }
    int query(int u, int v)
    {
        int re = inf;
        while(top[u] != top[v])
        {
            if(dep[top[u]] > dep[top[v]]) swap(u, v);
            re = min(re, query(1, 1, n + bcnt, dfn[top[v]], dfn[v]));
            v = anc[top[v]];
        }
        if(dep[u] > dep[v]) swap(u, v);
        re = min(re, query(1, 1, n + bcnt, dfn[u], dfn[v]));
        if(u > n)
        {
            re = min(re, val[anc[u]]);
        }
        return re;
    }
} seg;

void init()
{
    tarjan(1, 0);
    seg.init();
}

int main()
{
    // freopen("testdata.in", "r", stdin);
    read(n), read(m), read(q);
    for(int i = 1; i <= n; ++i)
    {
        read(val[i]);
    }
    memset(head, -1, sizeof(head));
    for(int i = 1; i <= m; ++i)
    {
        int u, v; read(u), read(v);
        addedge(u, v);
    }
    init();
    for(int i = 1; i <= q; ++i)
    {
        char op[5]; scanf("%s", op);
        if(op[0] == 'C')
        {
            int a, w; read(a), read(w);
            seg.update(a, w);
        }
        else
        {
            int a, b; read(a), read(b);
            printf("%d\n", seg.query(a, b));
        }
    }
    return 0;
}