Codeforces Round #408 (Div. 2) D
Description
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.
The first line contains three integers n, k, 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 ≤ n, ui ≠ 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.
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.
6 2 4
1 6
1 2
2 3
3 4
4 5
5 6
1
5
6 3 2
1 5 6
1 2
1 3
1 4
1 5
5 6
2
4 5
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.
题意:告诉你哪些点是警察局,由于资金原因需要删除一些道路,不过需要满足其他点到警察局的距离小于d
解法:BFS,其实d是无关的,我们只需要根据每个警察局进行BFS遍历,然后哪些路没走过就输出,(为什么d无关,我也不知道)
#include<bits/stdc++.h>
using namespace std;
int vis[];
int n,k,m,d;
int flag[];
queue<int>q;
vector<pair<int,int>>p[];
int main()
{
scanf("%d%d%d",&n,&m,&d);
for(int i=;i<=m;i++)
{
int num;
scanf("%d",&num);
vis[num]=;
q.push(num);
}
for(int i=;i<=n-;i++)
{
int u,v;
scanf("%d%d",&v,&u);
p[u].push_back({v,i});
p[v].push_back({u,i});
}
while(!q.empty())
{
int x=q.front();
q.pop();
// cout<<x<<endl;
for(int i=;i<p[x].size();i++)
{
int ans=p[x][i].first;
int cnt=p[x][i].second;
if(vis[ans]) continue;
vis[ans]=;
flag[cnt]=;
q.push(ans);
}
}
//cout<<endl;
int cot=;
for(int i=;i<n;i++)
{
if(!flag[i])
{
cot++;
}
}
printf("%d\n",cot);
for(int i=;i<n;i++)
{
if(!flag[i])
{
printf("%d ",i);
}
}
return ;
}
Codeforces Round #408 (Div. 2) D的更多相关文章
- 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 ...
- Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)
题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
- Codeforces Round #408 (Div. 2) C. Bank Hacking
http://codeforces.com/contest/796/problem/C Although Inzane successfully found his beloved bone, Zan ...
- Codeforces Round #408 (Div. 2) A B C 模拟 模拟 set
A. Buying A House time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #408 (Div. 2) D - Police Stations
地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...
- Codeforces Round #408 (Div. 2) B
Description Zane the wizard is going to perform a magic show shuffling the cups. There are n cups, n ...
- Codeforces Round #408 (Div. 2)
C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...
- Codeforces Round #408 (Div. 2) 题解【ABCDE】
A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...
- Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)
传送门 题意 n个点有n-1条边相连,其中有k个特殊点,要求: 删去尽可能多的边使得剩余的点距特殊点的距离不超过d 输出删去的边数和index 分析 比赛的时候想不清楚,看了别人的题解 一道将1个联通 ...
- Codeforces Round #408 (Div. 2) C.Bank Hacking(二分)
传送门 题意 给出n个银行,银行之间总共有n-1条边,定义i与j有边相连为neighboring,i到j,j到k有边,则定义i到k的关系为semi- neighboring, 每家银行hack的难度为 ...
随机推荐
- 检測磁盘驱动的健康程度SMART
在server中,全部组件中一般最easy坏掉的就是磁盘.所以一般採取RAID来保证系统的稳定性,通过冗余磁盘的方式防止磁盘故障. 现代硬件驱动器一般支持SMART(自我监測分析和报告技术),它可以监 ...
- Lambda Architecture
Lambda Architecture » λ lambda-architecture.net http://lambda-architecture.net/ Twitter's tweets ana ...
- 利用QBuffer和QLinkedList做数据块存储队列
Qt中QByteArray存储数据很方便,使用QBuffer存储大块数据更方便.QBuffer类包装了QByteArray类对象,实际存储还是使用了QByteArray,但QBuffer实现了QIOD ...
- iOS 在UILabel显示不同的字体和颜色(ios6 and later)
在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的at ...
- (C\C++)inline关键字
背景(C&C++中) inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义如: #define ExpressionName(Va ...
- dns服务器报错解决
搭了个dns服务器,配置完毕老是报错,这里总结一下常见思路: ①关闭firewalld ②关闭selinux ③/var/named里面的配置文件所属用户组是否是root:named ④named.c ...
- Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code
sonarqube的扫描结果提示 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creatin ...
- Codeforces-707D:Persistent Bookcase (离线处理特殊的可持久化问题&&Bitset)
Recently in school Alina has learned what are the persistent data structures: they are data structur ...
- [Selenium] Actions.doubleClick
WebElement el = page.getTable_AssetMixesName().get(index); Actions action = new Actions(driver); act ...
- python 使用multiprocessing需要注意的问题
我们在编写程序的时候经常喜欢这样写代码 import MySQLdb import time from multiprocessing import Process conn = MySQLdb.co ...