地址: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 ;
}

最新文章

  1. 【极品代码】一般人我不告诉他,手机端h5播放时不自动全屏代码
  2. QQ分组显示列表ExpandableListView组件应用源码
  3. django template中load的作用
  4. nginx的内存管理
  5. 【转】Paxos算法深入分析
  6. linux学习资料
  7. cf D. Dima and Hares
  8. devStack
  9. 【转载】【转自AekdyCoin的组合数取模】
  10. Java NIO Channel和Buffer
  11. 用java调用oracle存储过程总结
  12. .net 4.5 webform 提取ModelState错误信息
  13. Java多线程基础(二)
  14. SSM-Spring-01:Spring的概念+入门案例
  15. linux mint软件安装
  16. android studio java工程 报错
  17. Exception 04 : java.lang.ClassNotFoundException: Could not load requested class : org.hsqldb.jdbcDriver
  18. SSIS--(1)
  19. 【原创】QT 打印输出
  20. KMPlayer 一打开总是出现右面的窗口 导航区 怎样设置不会自动打开

热门文章

  1. [转]谈谈Linux下动态库查找路径的问题
  2. a标签点击后,给a标签添加样式
  3. centos7常用命令集合
  4. hdu1575 Tr A 矩阵快速幂模板题
  5. Net Core MVC6 RC2 启动过程分析
  6. 微信公众号获取用户openId How to use cURL to get jSON data and decode the data?
  7. 剑指Offer——把二叉树打印成多行
  8. ssh无密码登录设置
  9. PCI 设备详解一
  10. mysql 约束条件 auto_increment 自动增长 清空表 自动增长情况