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家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...
随机推荐
- 赵雅智_Swift(2)_swift常量和变量
分号 Swift 并不强制要求你在每条语句的结尾处使用分号(;) 你打算在同一行内写多条独立的语句必需要用分号 let cat = "? ?? ? "; println(cat) ...
- Bootstrap的表单控件
支持的表单控件 Bootstrap 支持最常见的表单控件,主要是 input.textarea.checkbox.radio 和 select. 输入框(Input) 最常见的表单文本字段是输入框 i ...
- sort-list——链表、快慢指针找中间、归并排序
Sort a linked list in O(n log n) time using constant space complexity. 链表,快慢指针找中点,归并排序. 注意判断条件fast-& ...
- 【分享】利用Apache的Htaccess Files命令限制訪问文件类型,Files正则
假设你在你的模板目录中有非常多PSD HTML模板,那么用接下来这个htaccess文件能够保护限制訪问: 文件D:\WebSite\ZBPHP.COM\www\Tpl\.htaccess 所有源代码 ...
- Excel实用技巧-如何批量提取excel工作表名称
Excel实用技巧-如何批量提取excel工作表名称 1. 打开Excel文件,点击“公式”栏,进而点击“定义管理器” 2. 在弹出的对话框中,点击新增按钮, 名称:“sheet”,引用位置:“=RE ...
- Page Design for Sexable Forum
Design Demo 1. Home of Sexable Forum 1.1 home page not logined. 1,2 home page logined. 2. Pages wit ...
- CXF发布在Web服务
1.下载apache-cxf-3.1.4,将jar引入新工程中. 2.People.java package com.soap.server; import java.util.Date; impor ...
- 【转】【录教程必备】推荐几款屏幕录制工具(可录制GIF)
我们经常会遇到一些场景,需要你向别人展示一些操作或是效果——例如告诉别人某某软件的配置步骤啊.刚设计出来网站的动画效果怎么样啊.某某电影里面的一个镜头多么经典啊.打得大快人心的NBA绝杀瞬间是怎么回事 ...
- access函数的使用检查文件的权限【学习笔记】
#include "apue.h" #include <fcntl.h> int main(int argc,char **argv) { ) err_quit(&qu ...
- hdu 超级楼梯 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 哦--对了,这些题读者可以直接忽略,我只是想练习一下自己薄弱的地方...... 题目意思我就不说 ...