Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)
传送门
题意
n个点有n-1条边相连,其中有k个特殊点,要求:
删去尽可能多的边使得剩余的点距特殊点的距离不超过d
输出删去的边数和index
分析
比赛的时候想不清楚,看了别人的题解
一道将1个联通块转化为k个树的题目,考虑上界,应该是k-1条边,这k-1条边是原图中连接树与树的边,那么我们操作如下:
1.将特殊点i染色为i,放入队列
2.做一次bfs,对每个点相邻的点染色并判断
3.遍历边,如果边的两点不同色,则输出边的index
trick
代码
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
using namespace std;
#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
#define cpy(a,b) memcpy(a,b,sizeof(b))
#pragma comment(linker, "/STACK:102400000,102400000")
inline void read(int &x){x=0; char ch=getchar();while(ch<'0') ch=getchar();while(ch>='0'){x=x*10+ch-48; ch=getchar();}}
int n,k,d,u,v;
vector<int>vec[300300];
vector<pair<int,int> >edg;
int f[300300],color[300300],dis[300300];
int cnt;
queue<int>q;
void bfs()
{
while(!q.empty())
{
int u=q.front();q.pop();
if(dis[u]==d) continue;
R(i,0,vec[u].size())
{
int v=vec[u][i];
if(!color[v])
{
color[v]=color[u];
dis[v]=dis[u]+1;
q.push(v);
}
}
}
}
int main()
{
scanf("%d %d %d",&n,&k,&d);
F(i,1,k)
{
int x;
scanf("%d",&x);
f[x]=1;
}
F(i,1,n-1)
{
scanf("%d %d",&u,&v);
edg.push_back(make_pair(u,v));
vec[u].push_back(v);
vec[v].push_back(u);
}
cnt=0;
F(i,1,n)if(f[i])
{
q.push(i);
color[i]=i;
cnt++;
}
bfs();
printf("%d\n",cnt-1);
int now=0;
//F(i,1,n) printf("color[%d]=%d\n",i,color[i]);
R(i,0,n-1)
{
int u=edg[i].first,v=edg[i].second;
if(color[u]!=color[v])
{
++now;
printf("%d%c",i+1,(now==(k-1))?'\n':' ');
}
}
return 0;
}
Codeforces Round #408 (Div. 2) D. Police Stations(最小生成树+构造)的更多相关文章
- 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 #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~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
Description Inzane finally found Zane with a lot of money to spare, so they together decided to esta ...
- 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 ...
- Codeforces Round #130 (Div. 2) C. Police Station
题目链接:http://codeforces.com/contest/208/problem/C 思路:题目要求的是经过1~N的最短路上的某个点的路径数 / 最短路的条数的最大值.一开始我是用spf ...
- 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家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
随机推荐
- jQuery -> 获取后代元素的三种方法
假设我们有内容例如以下的html文件,那么怎样选取包括在<p>元素内的<i>元素呢? 邪馬台国の謎と弥生時代 紀元前1000年ごろ.水稲工作の技術をもつ集団が大挙して日本に移住 ...
- NS3网络仿真(7): Wifi节点
快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 在上一节中.我们仿真了一个总线型网络,这一节尝试将上一节中的n0变成一个无线的AP.再连上几个节点 ...
- FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码(转)
调用的API为: String[] upload_file( String group_name,//组名,不指定则可设为null long file_size,//文件大小,必须制定 UploadC ...
- SpringMVC框架下使用jfreechart绘制折线图,柱状图,饼状图
java代码 @Controller public class CityAction { @Autowired private CityBiz cityBiz; //柱状图 @RequestMappi ...
- find the longest of the shortest (hdu 1595 SPFA+枚举)
find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others) Memory Limit: 32768/32768 ...
- VB.net版机房收费系统——结账功能实现(调错与优化)
调错部分 上一篇博客<VB.net版机房收费系统--结账功能实现(代码部分>说的是结账功能的实现,亮出了代码.是在为这篇博客做铺垫.尽管结账功能代码是借鉴的巨人的博客.可是自己比着葫芦画瓢 ...
- v-model在其它元素以及类型上的用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【C语言】统计数字在排序数组中出现的次数
//数字在排序数组中出现的次数. //统计一个数字在排序数组中出现的次数.比如:排序数组{1,2,3,3,3,3,4,5}和数字3,因为3出现了4次,因此输出4. #include <stdio ...
- debian old version cd and distribution archives
1 debian old version cd/dvd 官网的old version image,下载速度很慢 http://cdimage.debian.org/cdimage 下面这个靠谱,是镜像 ...
- Java中数组复制的几种方式以及数组合并
1.Object.clone() 简单直接,只能对源数组完整地复制 2.Arrays.copyOf(T[] original, int newLength) 可以只复制源数组中部分元素,但复制的起始位 ...