地址: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. hdu4734(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 思路:记忆化搜索. #include<iostream> #include<c ...

  2. chrome/FF 解析遇到 { 行为一致,返回不一致

    测试的时候,发现一个问题,FF下: chrome 下: 你会发现,FF 在解析一直到返回的时候,都是把 {x:1} 当做一个语句块去解析的,而 chrome 在返回的时候返回了对象,把 {x:1} 当 ...

  3. 微信公众号获取用户openId How to use cURL to get jSON data and decode the data?

    w http://stackoverflow.com/questions/16700960/how-to-use-curl-to-get-json-data-and-decode-the-data

  4. Ubuntu 分区以及各个挂载目录的基本含义

    我磁盘大概还有70多G的空间吧,我全部拿来使用的.真实的双系统哦. 一般来讲,linux系统分区最少要包括/和/swap两个.这样据说会影响性能,没有这样安装过,就无从考证啦.其实就是重装系统的时候, ...

  5. 剑指Offer——求1+2+3+...+n

    题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 分析: 递归实现. 代码: class So ...

  6. MapReduce分布式编程框架

    一.MapReduce分布式编程框架及yarn集群搭建 1.大数据解决的问题? 海量数据的存储:hadoop->分布式文件系统HDFS 海量数据的计算:hadoop->分布式计算框架Map ...

  7. git同步遇到报错“fatal: unable to access 'https://github.com/lizhong24/mysite2.git/': Peer reports incompatible or unsupported protocol version.”

    git同步遇到报错“fatal: unable to access 'https://github.com/lizhong24/mysite2.git/': Peer reports incompat ...

  8. mysql覆盖索引

    话说有这么一个表: CREATE TABLE `user_group` (   `id` int(11) NOT NULL auto_increment,   `uid` int(11) NOT NU ...

  9. android shareSDK实现第三方分享

    一.  http://www.mob.com/  在mob官网注册账号,获取ShareSDK的appkey,下载shareSDK并解压. 二.进入ShareSDK解压目录,打开“Share SDK f ...

  10. cordova 入门

    1. npm install -g cordova On Windows, npm can usually be found at C:\Users\username\AppData\Roaming\ ...