ZOJ red black tree

#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define lowbit(x) x&-x
#define clr(a) memset(a,0,sizeof(a))
#define _INF(a) memset(a,0x3f,sizeof(a))
#define FIN freopen("in.txt","r",stdin)
#define IOS ios::sync_with_stdio(false)
#define fuck(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MX = 1e5 + 7;

int n, m, q, _;
int red[MX];
struct edge
{
        int v, nxt;
        ll w;
} E[MX << 1];
int head[MX], tot, sz;
int id[MX << 1], dep[MX << 1], first[MX], ST[MX << 1][20], vis[MX], vec[MX];
ll dis[MX], d[MX];
bool cmp(int a, int b)
{
        return d[a] > d[b];
}
void init(int _n)
{
        for (int i = 0; i <= _n; i++)
        {
                head[i] = -1;
                vis[i] = red[i] = dis[i] = d[i] = 0;
        }
        tot = sz = 0;
}
void add_edge(int u, int v, ll w)
{
        E[tot].v = v;
        E[tot].w = w;
        E[tot].nxt = head[u];
        head[u] = tot++;
}
void dfs(int u, int deep, ll nw)
{
        if (red[u])
        {
                nw = 0;
        }
        d[u] = nw;
        vis[u] = 1;
        id[++sz] = u;
        first[u] = sz;
        dep[sz] = deep;
        for (int i = head[u]; ~i; i = E[i].nxt)
        {
                int v = E[i].v;
                ll w = E[i].w;
                if (vis[v])
                {
                        continue;
                }
                dis[v] = dis[u] + w;
                dfs(v, deep + 1, nw + w);
                id[++sz] = u;
                dep[sz] = deep;
        }
}
void ST_init(int _n)
{
        for (int i = 0; i <= _n; i++)
        {
                ST[i][0] = i;
        }
        for (int j = 1; (1 << j) <= _n; j++)
        {
                for (int i = 1; i + (1 << j) < _n; i++)
                {
                        int x = ST[i][j - 1], y = ST[i + (1 << (j - 1))][j - 1];
                        ST[i][j] = dep[x] < dep[y] ? x : y;
                }
        }
}
int RMQ(int l, int r)
{
        int k = 0;
        while ((1 << (k + 1)) <= r - l + 1)
        {
                k++;
        }
        int x = ST[l][k], y = ST[r - (1 << k) + 1][k];
        return dep[x] < dep[y] ? x : y;
}
int LCA(int u, int v)
{
        int x = first[u], y = first[v];
        if (x > y)
        {
                swap(x, y);
        }
        return id[RMQ(x, y)];
}

int main()
{
        //FIN;
        for (scanf("%d", &_); _; _--)
        {
                scanf("%d%d%d", &n, &m, &q);
                init(n);
                for (int i = 1, x; i <= m; i++)
                {
                        scanf("%d", &x);
                        red[x] = 1;
                }
                for (int i = 1; i < n; i++)
                {
                        int u, v, w;
                        scanf("%d%d%d", &u, &v, &w);
                        add_edge(u, v, w);
                        add_edge(v, u, w);
                }
                dfs(1, 1, 0);
                ST_init(sz);
                while (q--)
                {
                        int k;
                        scanf("%d", &k);
                        for (int i = 1; i <= k; i++)
                        {
                                scanf("%d", &vec[i]);
                        }
                        sort(vec + 1, vec + k + 1, cmp);
                        ll ans = d[vec[1]], lastcnt = 0;
                        int lastlca = vec[1];
                        for (int i = 1; i <= k; i++)
                        {
                                int lca = LCA(lastlca, vec[i]);
                                ll res1 = lastcnt + dis[lastlca] - dis[lca];
                                if (i > 1 && res1 >= d[vec[i - 1]])
                                {
                                        break;
                                }
                                ll res2 = min(d[vec[i]], dis[vec[i]] - dis[lca]);
                                ll cnt = max(res1, res2);
                                if (cnt >= ans)
                                {
                                        break;
                                }
                                if (i + 1 <= k)
                                {
                                        ans = min(ans, max(cnt, d[vec[i + 1]]));
                                }
                                else
                                {
                                        ans = min(ans, cnt);
                                }
                                lastlca = lca;
                                lastcnt = cnt;
                        }
                        printf("%lld\n", ans);
                }
        }
        return 0;
}
View Code

 

posted @ 2018-09-18 20:26  Aragaki  阅读(167)  评论(0编辑  收藏  举报