题解 P1967 【货车运输】

· · 题解

表示对倍增的原理一直理解不能。。。。

于是开心的写了一下链剖

首先就是建出最大生成树

把边权化一下点权

然后考虑用什么数据结构维护了。。

由于本人弱鸡,只会用线段树(貌似st表的空间会大很多?)

所以问题就转为了求区间最小值

记得查询时先判断是否两点是否在同一联通块内(并查集)

(指针大法好!!!)

#include <cstdio>
#include <iostream>
#include <algorithm>
const int BUF = 123123123;
char Buf[BUF], *buf = Buf;

inline void read (int &now)
{
    for (now = 0; !isdigit (*buf); ++ buf);
    for (; isdigit (*buf); now = now * 10 + *buf - '0', ++ buf);
}

#define INF 1e9
#define Max 30100

struct E
{
    E *n;
    int to, key;
};
E *list[Max], poor[Max], *Tail = poor;

struct Edge
{
    int from, to, key;
    bool operator < (const Edge &now) const
    {
        return this->key > now.key;
    }
};
Edge data[Max * 5];

struct Unio_Find
{
    protected : int data[Max];
    public :

        void Prepare (const int &N)    
        {
            for (register int i = 1; i <= N; ++ i) 
                data[i] = i;
        }

        int Find (int x) 
        {    
            return data[x] == x ? x : data[x] = Find (data[x]);
        }

        inline void Unio (int x, int y)    { data[x] = y; }
};
Unio_Find Ufs;
inline int min (int a, int b)
{
    return a < b ? a : b;
}
int N, M;
struct S_D
{
    S_D *Left, *Right;
    int key, Mid;
    inline void Up ()
    {
        this->key = min (this->Left->key, this->Right->key);
    }
};
int tree_key[Max], in[Max];

inline void swap (int &a, int &b)
{
    int now = a; a = b, b = now;
}

class Segment_Tree
{
    private : S_D _poor[Max], *Tail, *Root;

        inline S_D *New (int x, int y)
        {
            ++ this->Tail, this->Tail->Mid = x + y >> 1, this->Tail->key = 0;
            this->Tail->Left = this->Tail->Right = NULL; return this->Tail;
        }    

        void Build (S_D *&now, int l, int r)
        {
            now = New (l, r);
            if (l == r)
            {
                now->key = tree_key[l];
                return ;
            }
            Build (now->Left, l, now->Mid);
            Build (now->Right, now->Mid + 1, r);
            now->Up ();
        }

        int Query (S_D *&now, int L, int R, int l, int r)
        {
            if (l <= L && R <= r)
                return now->key;
            int res = INF;
            if (l <= now->Mid) res = Query (now->Left, L, now->Mid, l, r);
            if (r > now->Mid) 
                res = min (res, Query (now->Right, now->Mid + 1, R, l, r));
            return res;
        }

    public :

        Segment_Tree () { this->Tail = _poor;}

        void Build (int l, int r) { return Build (Root, l, r); }
        int Query (int l, int r) { return Query (Root, 1, N, l, r); }
};
Segment_Tree Seg;

class Tree_Chain_Get_Type
{
    private : 

        int deep[Max], size[Max], father[Max], chain[Max], son[Max];
        int C;

        void Dfs_1 (int now, int Father)
        {
            size[now] = 1, father[now] = Father, deep[now] = deep[Father] + 1;
            for (E *e = list[now]; e; e = e->n)
                if (e->to != Father)
                {
                    Dfs_1 (e->to, now); size[now] += size[e->to];
                    if (size[son[now]] < size[e->to]) son[now] = e->to;
                }
        }

        void Dfs_2 (int now, int point)
        {
            chain[now] = point; in[now] = ++ C; E *e;
            for (e = list[now]; e; e = e->n)
                if (e->to == father[now])
                    tree_key[C] = e->key;
            if (son[now]) Dfs_2 (son[now], point);
            else return ;    
            for (e = list[now]; e; e = e->n)
                if (e->to != son[now] && e->to != father[now])
                    Dfs_2 (e->to, e->to);
        }

    public :

        void Prepare ()
        {
            C = 0, Dfs_1 (1, 0), Dfs_2 (1, 1); Seg.Build (1, N);

        }

        int Query (int x, int y)
        {
            if (Ufs.Find (x) != Ufs.Find (y)) return -1;
            int res = INF;    
            for (; chain[x] != chain[y]; )
            {
                if (deep[chain[x]] < deep[chain[y]]) swap (x, y);
                res = min (Seg.Query (in[chain[x]], in[x]), res);    
                x = father[chain[x]];
            }
            if (deep[x] > deep[y]) swap (x, y);
            if (x != y) res = min (res, Seg.Query (in[x] + 1, in[y]));
            return res;
        }
};
Tree_Chain_Get_Type T;

int Main ()
{

    fread (buf, 1, BUF, stdin);

    read (N), read (M);
    int x, y, z;    
    register int i;
    for (i = 1; i <= M; ++ i)
    {
        read (x), read (y), read (z);
        data[i].from = x, data[i].to = y, data[i].key = z;
    }
    std :: sort (data + 1, data + 1 + M);
    int Count = 0; Ufs.Prepare (N);
    for (i = 1; i <= M; ++ i)
    {
        x = Ufs.Find (data[i].from), y = Ufs.Find (data[i].to);
        if (x != y)
        {
            ++ Tail, Tail->to = data[i].to, Tail->key = data[i].key;
            Tail->n = list[data[i].from], list[data[i].from] = Tail;
            ++ Tail, Tail->to = data[i].from, Tail->key = data[i].key;
            Tail->n = list[data[i].to], list[data[i].to] = Tail;
            ++ Count; Ufs.Unio (x, y);
        }
        if (Count == N - 1) break;
    }
    T.Prepare ();
    int Q; read (Q);
    for (i = 1; i <= Q; ++ i)
        read (x), read (y), printf ("%d\n", T.Query (x, y));            
    return 0;
}
int ZlycerQan = Main ();
int main (int argc, char *argv[]) {;}