地址:http://codeforces.com/contest/796/problem/D

题目:

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·105, 1 ≤ k ≤ 3·105, 0 ≤ 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 4
1 6
1 2
2 3
3 4
4 5
5 6
output
1
5
input
6 3 2
1 5 6
1 2
1 3
1 4
1 5
5 6
output
2
4 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.

思路:多起点bfs,当bfs到两个相连的点u,v && dis[u]<=d&&dis[v]<=d时则说明该边可以删除。

  bfs过程中hs一下路径就好了

  代码交了两发,一次1980ms,一次1996ms,,感觉再交几次就可能t了

  其实这题就是个多原点最小生成树,可以去掉map优化。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=3e5+;
const int mod=1e9+; map<PII,int>hs;
queue<int>q;
vector<int>mp[K];
set<int>sa,sb;
int n,k,d,ans,dis[K],v[K];
void bfs(void)
{
while(q.size())
{
for(int k=,sz=q.size();k<sz;k++)
{
int x=q.front();q.pop();
for(int i=;i<mp[x].size();i++)
{
int u=mp[x][i],f=hs[MP(x,u)];
if(!f)f=hs[MP(u,x)];
if(sa.find(f)!=sa.end()||sb.find(f)!=sb.end())
continue;
if(dis[u]<=d&&dis[x]<=d)
{
ans++,sb.insert(f);
continue;
}
if(dis[u]>d) dis[u]=dis[x]+,q.push(u),sa.insert(f);
}
}
}
}
int main(void)
{
cin>>n>>k>>d;
memset(dis,0x3f3f3f3f,sizeof dis);
for(int i=,x;i<=k;i++)
scanf("%d",&x),q.push(x),dis[x]=;
for(int i=,x,y;i<n;i++)
scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x),hs[MP(x,y)]=i;
bfs();
cout<<ans<<endl;
for(auto x:sb)
printf("%d ",x);
return ;
}

Codeforces Round #408 (Div. 2) D - Police Stations的更多相关文章

  1. Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)

    传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...

  2. Codeforces Round #408 (Div. 2)

    C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...

  3. Codeforces Round #408 (Div. 2) 题解【ABCDE】

    A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...

  4. Codeforces Round #408 (Div. 2) D

    Description Inzane finally found Zane with a lot of money to spare, so they together decided to esta ...

  5. Codeforces Round #130 (Div. 2) C - Police Station 最短路+dp

    题目链接: http://codeforces.com/problemset/problem/208/C C. Police Station time limit per test:2 seconds ...

  6. Codeforces Round #130 (Div. 2) C. Police Station

    题目链接:http://codeforces.com/contest/208/problem/C 思路:题目要求的是经过1~N的最短路上的某个点的路径数 /  最短路的条数的最大值.一开始我是用spf ...

  7. Codeforces Round #408 (Div. 2)(A.水,B,模拟)

    A. Buying A House time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  8. Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)

    题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...

  9. Codeforces Round #408 (Div. 2) C. Bank Hacking

    http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...

随机推荐

  1. python 发送email

    pyton smtplib发送邮件 在邮件中设置并获取到 smtp域名 在脚本中执行命名,收件人可以是 多个,在列表中 import smtplib from email.mime.text impo ...

  2. 自己实现一个Promise库

    源码地址 先看基本使用 const promise = new Promise((resolve, reject) => { resolve(value) // or reject(reason ...

  3. 编程之美 set 21 24点游戏

    题目 输入: n1, n2, n3, n4 (1~13) 输出: 若能得到运算结果为 24, 则输出一个对应的运算表达式 如: 输入: 11, 8, 3, 5 输出: (11-8) * (3*5) = ...

  4. JSP内置对象——out,get与post

    JSP内置对象是Web容器创建的一组对象,不使用new关键字就可以的内置对象.JSP九大内置对象:out,request,response,session,application,page,pageC ...

  5. codevs 5962 [SDOI2017]数字表格

    输入描述 Input Description  [题解] 对于蓝色部分预处理前缀积. 然后在用除法分块搞一下. O(Q*sqrt(min(n,m))*logn+nlogn) #include<c ...

  6. 【BZOJ4155】[Ipsc2015]Humble Captains 最小割+DP

    [BZOJ4155][Ipsc2015]Humble Captains Description 每天下午放学时都有n个zky冲出教室去搞基.搞基的zky们分成两队,编号为1的zky是1号队的首领,编号 ...

  7. 【Python数据挖掘】决策树、随机森林、Bootsing、

    决策树的定义 决策树(decision tree)是一个树结构(可以是二叉树或非二叉树).其每个非叶节点表示一个特征属性上的测试,每个分支代表这个特征属性在某个值域上的输出,而每个叶节点存放一个类别. ...

  8. 苏宁易购微信端 wx ios android other 通过js来控制样式

    <!DOCTYPE HTML><html><head><meta charset="UTF-8"><meta name=&qu ...

  9. librosa音频特征提取,python librosa库在centos上依赖llvm的问题?

    win10下安装使用: https://blog.csdn.net/qq_39516859/article/details/80679718 https://blog.csdn.net/qq_3951 ...

  10. scrollend,滚动结束执行一次

    var timer;window.onscroll = function () { clearTimeout(timer); timer = setTimeout(function () { aler ...