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家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
随机推荐
- 零售连锁行业SOA化解决方式
零售连锁行业面临的问题 1.店铺老化.营销手段单一落后. 2.管理模式的不科学,我国零售企业在起家时候并没有一套完好的科学的管理及考评系统的存在.而且在企业的发展过程中并未学习建立.对于人才的吸引.培 ...
- HDU2084_数塔【简单题】【数塔】
数塔 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...
- mybatis xml文件解析
1 parameterType 如果参数只有一个,比如一个id,即int类型的id,那么parameterType直接是int. 如果参数有多个,那么就用表中一行对应的类,默认是类的名字和表中列的名字 ...
- 登录日志的访问日志的 统计 MapReduce
登录日志的访问日志的 统计 MapReduce <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-commo ...
- YARN commands are invoked by the bin/yarn script.
Apache Hadoop 2.9.0 – YARN Commands http://hadoop.apache.org/docs/stable/hadoop-yarn/hadoop-yarn-sit ...
- javascript if(xx)
js判断某个值知否存在或者为空,可以直接用if(xx)过滤. <!DOCTYPE html> <html> <head> <meta charset=&quo ...
- linunx 下载文件到本地
1.检查并安装相应的包:yum install lrzsz 2.使用 sz 文件名 现在相应的文件到本地磁盘. 3.上传使用rz 选择相应文件即可.
- 【转】使用git 工具下载android.jar Source Code
为了开发android应用,在开发时发现sdk没有源代码,这样在开发时太麻烦了,下面说说如何下载源代码,以及如何配置. 下载源代码需要git,先下载一个git.下面的操作都是在windows下完成的. ...
- bzoj4149: [AMPPZ2014]Global Warming
头都烂了怎么头疼啊 考虑先做出对于一个位置以它作为唯一最小值的最远区间,这个可以单调栈上二分搞出来 那么对于一个位置这个区间而言,一定是选择这个区间的最大数是作为最终的唯一最大数最优的 为什么呢?我们 ...
- kentico检查当前授权用户,是否为admin角色
https://docs.kentico.com/k11/custom-development/user-internals-and-api/checking-permissions-using-th ...