Codeforces 588E Duff in the Army 【树链剖分维护区间前k小】

题目链接:Codeforces 588E Duff in the Army

E. Duff in the Army
time limit per test4 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Recently Duff has been a soldier in the army. Malek is her commander.

Their country, Andarz Gu has n cities (numbered from 1 to n) and n - 1 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities.

There are also m people living in Andarz Gu (numbered from 1 to m). Each person has and ID number. ID number of i - th person is i and he/she lives in city number ci. Note that there may be more than one person in a city, also there may be no people living in the city.

Malek loves to order. That’s why he asks Duff to answer to q queries. In each query, he gives her numbers v, u and a.

To answer a query:

Assume there are x people living in the cities lying on the path from city v to city u. Assume these people’s IDs are p1, p2, …, px in increasing order.

If k = min(x, a), then Duff should tell Malek numbers k, p1, p2, …, pk in this order. In the other words, Malek wants to know a minimums on that path (or less, if there are less than a people).

Duff is very busy at the moment, so she asked you to help her and answer the queries.

Input
The first line of input contains three integers, n, m and q (1 ≤ n, m, q ≤ 105).

The next n - 1 lines contain the roads. Each line contains two integers v and u, endpoints of a road (1 ≤ v, u ≤ n, v ≠ u).

Next line contains m integers c1, c2, …, cm separated by spaces (1 ≤ ci ≤ n for each 1 ≤ i ≤ m).

Next q lines contain the queries. Each of them contains three integers, v, u and a (1 ≤ v, u ≤ n and 1 ≤ a ≤ 10).

Output
For each query, print numbers k, p1, p2, …, pk separated by spaces in one line.

Examples
input
5 4 5
1 3
1 2
1 4
4 5
2 1 4 3
4 5 6
1 5 2
5 5 10
2 3 3
5 3 1
output
1 3
2 2 3
0
3 1 2 4
1 2
Note
Graph of Andarz Gu in the sample case is as follows (ID of people in each city are written next to them):

题意:给定一棵树,每个节点上有若干个编号(1-m),每个编号只存在一个节点上。有q次查询,问你从u->v的路径上前k小的编号,升序输出。

思路:很自然的想到树链剖分,关键在于维护前10小的编号。我们可以直接归并排序去维护区间前10小的编号,写的又臭又长。。。。。。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Tree{
    int l, r;
    int Q[10], cnt;
};
Tree tree[MAXN<<2];
void PushUp(int o) {
    int os = 0, ls = tree[ll].cnt, rs = tree[rr].cnt;
    int lc = 0, rc = 0;
    while(os < 10) {
        if(lc < ls && rc < rs) {
            if(tree[ll].Q[lc] > tree[rr].Q[rc]) {
                tree[o].Q[os++] = tree[rr].Q[rc++];
            }
            else {
                tree[o].Q[os++] = tree[ll].Q[lc++];
            }
        }
        else if(lc < ls) {
            tree[o].Q[os++] = tree[ll].Q[lc++];
        }
        else if(rc < rs) {
            tree[o].Q[os++] = tree[rr].Q[rc++];
        }
        else break;
    }
    tree[o].cnt = os;
}
priority_queue<int, vector<int>, greater<int> > QQ[MAXN];
void Build(int o, int l, int r)
{
    tree[o].l = l; tree[o].r = r;
    tree[o].cnt = 0;
    if(l == r) {
        return ;
    }
    int mid = (l + r) >> 1;
    Build(ll, l, mid); Build(rr, mid+1, r);
}
void Update(int o, int index, int pos) {
    if(tree[o].l == tree[o].r) {
        int os = 0;
        while(!QQ[index].empty() && os < 10) {
            tree[o].Q[os++] = QQ[index].top();
            QQ[index].pop();
        }
        tree[o].cnt = os;
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(pos <= mid) Update(ll, index, pos);
    else Update(rr, index, pos);
    PushUp(o);
}
int ans[10], ans1[10], res;
void Query(int o, int L, int R)
{
    if(tree[o].l == L && tree[o].r == R) {
        memcpy(ans1, ans, sizeof(ans));
        int os = 0, ls = res, rs = tree[o].cnt;
        int lc = 0, rc = 0;
        while(os < 10) {
            if(lc < ls && rc < rs) {
                if(tree[o].Q[rc] > ans1[lc]) {
                    ans[os++] = ans1[lc++];
                }
                else {
                    ans[os++] = tree[o].Q[rc++];
                }
            }
            else if(lc < ls) {
                ans[os++] = ans1[lc++];
            }
            else if(rc < rs) {
                ans[os++] = tree[o].Q[rc++];
            }
            else break;
        }
        res = os;
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid) Query(ll, L, R);
    else if(L > mid) Query(rr, L, R);
    else { Query(ll, L, mid), Query(rr, mid+1, R); }
}
struct Edge {
    int from, to, next;
};
Edge edge[MAXN<<1];
int head[MAXN], edgenum;
void init() {
    edgenum = 0; CLR(head, -1);
}
void addEdge(int u, int v)
{
    Edge E = {u, v, head[u]};
    edge[edgenum] = E;
    head[u] = edgenum++;
}
int son[MAXN], num[MAXN];
int top[MAXN], pos[MAXN], id;
int dep[MAXN], pre[MAXN];
void DFS1(int u, int fa, int d)
{
    dep[u] = d; pre[u] = fa; num[u] = 1; son[u] = -1;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == fa) continue;
        DFS1(v, u, d+1);
        num[u] += num[v];
        if(son[u] == -1 || num[son[u]] < num[v])
            son[u] = v;
    }
}
void DFS2(int u, int T)
{
    top[u] = T; pos[u] = ++id;
    if(son[u] == -1) return ;
    DFS2(son[u], T);
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if(v == pre[u] || v == son[u]) continue;
        DFS2(v, v);
    }
}
void Solve(int u, int v)
{
    int f1 = top[u], f2 = top[v];
    res = 0;
    while(f1 != f2)
    {
        if(dep[f1] < dep[f2]) {
            swap(u, v); swap(f1, f2);
        }
        Query(1, pos[f1], pos[u]);
        u = pre[f1], f1 = top[u];

    }
    if(dep[u] > dep[v]) swap(u, v);
    Query(1, pos[u], pos[v]);
}
int main()
{
    int n, m, q;
    while(scanf("%d%d%d", &n, &m, &q) != EOF) {
        init();
        for(int i = 0; i < n-1; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            addEdge(u, v); addEdge(v, u);
        }
        DFS1(1, -1, 1); id = 0; DFS2(1, 1); Build(1, 1, id);
        for(int i = 1; i <= m; i++) {
            int v; scanf("%d", &v);
            if(QQ[v].size() < 10) {
                QQ[v].push(i);
            }
        }
        for(int i = 1; i <= n; i++) {
            Update(1, i, pos[i]);
        }
        while(q--) {
            int u, v, k; scanf("%d%d%d", &u, &v, &k);
            Solve(u, v); int temp = min(k, res);
            printf("%d", temp);
            for(int i = 0; i < temp; i++) {
                printf(" %d", ans[i]);
            }
            printf("\n");
        }
    }
    return 0;
}

你可能感兴趣的:(Codeforces 588E Duff in the Army 【树链剖分维护区间前k小】)