D. Police Stations----多起点bfs

来源:互联网 发布:虚拟机软件有哪些 知乎 编辑:程序博客网 时间:2024/06/06 09:40

D. Police Stations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.

Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads.

There are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.

However, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.

Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.

Input

The first line contains three integers nk, and d (2 ≤ n ≤ 3·1051 ≤ k ≤ 3·1050 ≤ d ≤ n - 1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.

The second line contains k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — each denoting the city each police station is located in.

The i-th of the following n - 1 lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities directly connected by the road with index i.

It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.

Output

In the first line, print one integer s that denotes the maximum number of roads that can be shut down.

In the second line, print s distinct integers, the indices of such roads, in any order.

If there are multiple answers, print any of them.

Examples
input
6 2 41 61 22 33 44 55 6
output
15
input
6 3 21 5 61 21 31 41 55 6
output
24 5 
Note

In the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers.

In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line.


题目链接:http://codeforces.com/contest/796/problem/D


题目的意思就是有一些特殊点,要求所有点距离特殊点的距离不能超过k,问你最多能去掉几条边。


我对于这个题除了暴力并没有任何的思路,然后无奈的我上网看了题解,多起点bfs。

给出大神的思路:

根据题意可以知道只要不删除边就必定满足条件,那么只要暴力把全部特殊点放进队列广搜一边就好了。若当前边走过就返回,若当前边没走过且下一个点走过那么这条边是可以删除的。

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <queue>#define LL long longusing namespace std;const int maxn=300005;int vis[maxn];queue<int >Q;struct node{    int v,next;}p[maxn<<1];int head[maxn],len,vis1[maxn],vis2[maxn],ans[maxn],sum;void connect(int u,int v){    p[len].v=v;    p[len].next=head[u];    head[u]=len++;}void bfs(){    while(!Q.empty()){        int now=Q.front();        Q.pop();        for(int i=head[now];~i;i=p[i].next){            if(vis2[i/2+1])                continue;            vis2[i/2+1]=1;            if(vis1[p[i].v])                ans[sum++]=i/2+1;            else{                vis1[p[i].v]=1;                Q.push(p[i].v);            }        }    }}int main(){    int n,m,k;    memset(head,-1,sizeof(head));    scanf("%d%d%d",&n,&m,&k);    for(int i=0;i<m;i++){        int x;        scanf("%d",&x);        vis1[x]=1;        Q.push(x);    }    for(int i=0;i<n-1;i++){        int u,v;        scanf("%d%d",&u,&v);        connect(u,v);        connect(v,u);    }    bfs();    printf("%d\n",sum);    for(int i=0;i<sum;i++){        printf(i==0?"%d":" %d",ans[i]);    }    cout<<endl;    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 收割机过桥吐麦怎么办 德牧不吃东西怎么办 摩托车头盔太重怎么办 摩托车头盔起雾怎么办 真皮沙发太软了怎么办 老师硬让换班怎么办 同事经常要换班怎么办 怀孕在家没钱花怎么办 摩托车车把歪了怎么办 摩旅 手机不防水怎么办 头盔镜片花了怎么办 踏板摩托车速度底怎么办 摩托车头盔小了怎么办 房东和租客纠纷怎么办 租客与房东纠纷怎么办 乙肝打了瘦脸针怎么办 去绣水搞到手上痛怎么办 脚破了皮很痛怎么办 脚被车撞了肿了怎么办 ps4光盘花了怎么办 耳后总是长孑子怎么办 孩孑高三总是玩手机怎么办 摩托车被收了怎么办 摩托车的手续都怎么办 摩托车罚单掉了怎么办 行人遇到黄灯该怎么办 长辈借钱不还怎么办 不绣刚电梯轿壁有凹槽怎么办 电梯下限位故障怎么办 卫生间夏天太热怎么办 07大檐帽变形了怎么办 税务局不批发票怎么办 进项发票太多了怎么办 发票报销联丢失怎么办 发票领用簿没有怎么办 发票购买本遗失怎么办 销售方遗失发票怎么办 增值税发票发票联丢失怎么办 苹果购买发票丢失怎么办 空白增值税发票发票丢失怎么办 网购发票 领购簿怎么办