地址: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. 使用ADO GetChunk/AppendChunk 数据库存取二进制文件图象

    在设计数据库的过程中,我们会经常要存储一些图形.长文本.多媒体(视频.音频文件)等各种各样的程序文件,如果我们在数据库中仅存储这些文件的路径信息,尽管这可以大大地减小数据库的大小,但是由于文件存在磁盘 ...

  2. windows CMD命令大全及详细解释和语法

    http://blog.csdn.net/god_7z1/article/details/51173067

  3. mac os x 记录 转载

    转载:远景网友(手机锋友t5sd3sf):http://bbs.feng.com/read-htm-tid-10434256.html 一个命令制作 OS X 原版安装U盘 1.要保证下载的原版安装包 ...

  4. Amazon(iam)IAM用户启用MFA

    1.1 下载MFA软件 我这里选择Google的Authenticator 1.2 进入IAM 搜索IAM,点击进入 1.3 选择需要设置MFA的用户 1.4 选择安全证书 1.5 管理MFA设置 这 ...

  5. FAIL : SSHException: Incompatible ssh peer (no acceptable kex algorithm)问题解决及更新paromiko失败问题解决

    转自:http://blog.csdn.net/qy924565830/article/details/52164256 http://blog.csdn.net/coder_xia/article/ ...

  6. egret.Capabilities 在pc和移动端输出值

    egret.log("Device:", egret.Capabilities.os, App.DeviceUtils.IsWeb, App.DeviceUtils.isMobil ...

  7. iOS json解析中包含“\n”等解析出错

    文题算是解决了,把特殊字符替换一下:-(NSString *)JSONString:(NSString *)aString {    NSMutableString *s = [NSMutableSt ...

  8. HDU 5667 Sequence(矩阵快速幂)

    Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...

  9. Delphi开发的服务在Windows2003 64位注册方式。

    1.在sysWoW32目录下找到cmd.exe,右键运行方式选择administrator,输入密码后.2.TrainServer.exe -install  安装服务.

  10. Exchange Pause or stop transport service

    The Microsoft Exchange Transport service is a service available both on the Microsoft Exchange Serve ...