频道栏目
首页 > 程序开发 > 软件开发 > 其他 > 正文
Duff in the Army codeforces 588E 树上主席树+lca
2017-08-04 10:32:08      个评论    来源:Hallelujah520的博客  
收藏   我要投稿

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.

Example
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):

一开始看错题了= =题意是:给一棵树n个点,再给m个人,说明m个人所在的点同时每个人都有一个编号i,
你的任务就是找到 u v这条路的前k个人,输出其编号。

树上主席书+lca

一开始写的版本:

#include 
using namespace std;
const int N = 1e5+100;
int n,m,q;
int sum[N*40],ls[N*40],rs[N*40],root[N];
int f[N][20],dep[N];
vector e[N],v,num[N];
int sze;

void update(int pre,int &now,int l,int r,int d,int val)
{
    now=++sze;
    ls[now]=ls[pre],rs[now]=rs[pre];
    sum[now]=sum[pre]+val;
    if(l==r) return ;
    int mid=(l+r)>>1;
    if(d<=mid) update(ls[pre],ls[now],l,mid,d,val);
    else update(rs[pre],rs[now],mid+1,r,d,val);
}


void dfs(int x,int y)
{
    //build(root[y],root[x],1,m); 本来这样写的,但是这样写会re
    root[x]=root[y];//复制上一棵树,不用重新build,获取头节点就好
    if(num[x].size())
    {
    for(int i=0;i=0;i--)
        if(f[x][i]!=f[y][i])
            x=f[x][i],y=f[y][i];
    if(x==y)return x;
    return f[x][0];
}

void ask(int aa,int bb,int cc,int dd,int l,int r,int k)
{
       // cout<
点击复制链接 与好友分享!回本站首页
上一篇:编程开发计算题. 月饼 (25)
下一篇:给按钮注册事件监听器
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站