传送门

题意

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(最小生成树+构造)的更多相关文章

  1. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  2. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  3. Codeforces Round #408 (Div. 2)

    C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...

  4. Codeforces Round #408 (Div. 2) 题解【ABCDE】

    A - Buying A House 题意:给你n个房间,妹子住在第m个房间,你有k块钱,你想买一个离妹子最近的房间.其中相邻的房间之间距离为10,a[i]=0表示已经被别人买了. 题解:扫一遍更新答 ...

  5. Codeforces Round #408 (Div. 2) D

    Description Inzane finally found Zane with a lot of money to spare, so they together decided to esta ...

  6. 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 ...

  7. Codeforces Round #130 (Div. 2) C. Police Station

    题目链接:http://codeforces.com/contest/208/problem/C 思路:题目要求的是经过1~N的最短路上的某个点的路径数 /  最短路的条数的最大值.一开始我是用spf ...

  8. 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 ...

  9. Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)

    题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...

随机推荐

  1. shell 检查文件夹所属用户组

    shell 检查文件夹所属用户组 #!/bin/bash # 检查文件夹 权限是否777 检查文件夹所属组是否www # authro ranmufei # 2017 08 21 云板容器版 /dat ...

  2. RPM安装mysql5.6

    原文 http://blog.csdn.net/liumm0000/article/details/18841197 a. 检查MySQL及相关RPM包,是否安装,如果有安装,则移除(rpm –e 名 ...

  3. AMD的ARM之路前景几何?

    http://server.zdnet.com.cn/all-2129330.html#2129333 AMD将于2014年推出基于ARM架构的Opteron(皓龙)处理器,应该是最近一段时间在IT产 ...

  4. Linux和Windows设备驱动架构比较

    毕业后一直在学操作系统, 有时候觉得什么都懂了,有时候又觉得好像什么都不懂,但总体来说自认为对操作系统实现机制的了解比周围的人还是要多一些.去年曾花了几个星期的晚上时间断断续续翻译了这篇对Linux和 ...

  5. (转)typedef用法

    Typedef 声明有助于创建平台无关类型,甚至能隐藏复杂和难以理解的语法.不管怎样,使用 typedef 能为代码带来意想不到的好处,通过本文你可以学习用 typedef 避免缺欠,从而使代码更健壮 ...

  6. ln: 正在创建指向“asm-arm”的符号链接“asm”: 不支持的操作

    原因是不能在windows共享目录编译,将待编译的uboot源码copy到home目录

  7. 基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有測试代码(何志雄)

    转载请标明出处. 在分布式系统中,常常会出现须要竞争同一资源的情况,本代码基于redis3.0.1+jedis2.7.1实现了分布式锁. redis集群的搭建,请见我的另外一篇文章:<>& ...

  8. unity 3D Mesh网络模型,怎样将Constructer拖入场景??

    下图中的将Constructer拖入场景,怎么拖入,不知道... 1.Constructer是一个什么东西?在 下图中没有看到这个名字的,于是乎,我就不知道该怎么办了...

  9. DTD复习笔记(复习资料为菜鸟教程里的DTD教程)

    DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块. DTD 可被成行地声明于 XML 文档中,也可作为一个外部引用. 为什么使用 DTD? 通过 DTD,您的每一个 XML 文件均可携带 ...

  10. web 前端冷知识

    前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...