[HDU6203]ping ping ping
题目大意:
给你一棵树,其中有一些点是坏掉的。告诉你k个点对表示这两个点的路径上至少有1个点是坏掉的。问整棵树上至少有多少点是坏的。
思路:
贪心。
找出每组点对的LCA,对所有点对按照LCA的深度排序。
然后枚举每一组点对,如果当前的两个结点u和v都没有被标记,则把以其LCA为根的子树标记成坏的,并将LCA算入答案。
如果当前的两个结点u和v至少有一个被标记,则说明两个结点已经不连通,不需要将其LCA计入答案。
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<algorithm>
#include<functional>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int V=,logV=;
std::vector<int> e[V];
inline void addedge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
int dep[V],anc[V][logV];
inline int _log2(const float &x) {
return ((unsigned&)x>>&)-;
}
void dfs(const int &x,const int &p) {
dep[x]=dep[p]+;
anc[x][]=p;
for(int i=;i<=_log2(dep[x]);i++) {
anc[x][i]=anc[anc[x][i-]][i-];
}
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==p) continue;
dfs(y,x);
}
}
inline int getlca(int x,int y) {
if(dep[x]<dep[y]) std::swap(x,y);
for(register int i=_log2(dep[x]);i>=;i--) {
if(dep[anc[x][i]]>=dep[y]) {
x=anc[x][i];
}
}
if(x==y) return x;
for(register int i=_log2(dep[x]);i>=;i--) {
if(anc[x][i]!=anc[y][i]) {
x=anc[x][i];
y=anc[y][i];
}
}
return anc[x][];
}
struct Path {
int u,v,lca;
Path(const int &u,const int &v,const int &lca) {
this->u=u;
this->v=v;
this->lca=lca;
}
bool operator > (const Path &another) const {
return dep[lca]>dep[another.lca];
}
};
std::vector<Path> lca;
bool mark[V];
void modify(const int &x) {
if(mark[x]) return;
mark[x]=true;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==anc[x][]) continue;
modify(y);
}
e[x].clear();
}
inline void init() {
for(register int i=;i<V;i++) e[i].clear();
lca.clear();
memset(mark,,sizeof mark);
}
int main() {
register int n;
while(~scanf("%d\n",&n)) {
init();
for(;n;n--) {
addedge(getint(),getint());
}
dfs(,);
for(register int k=getint();k;k--) {
const int &u=getint(),&v=getint();
lca.push_back(Path(u,v,getlca(u,v)));
}
std::sort(lca.begin(),lca.end(),std::greater<Path>());
int ans=;
for(register unsigned i=;i<lca.size();i++) {
const int &u=lca[i].u,&v=lca[i].v,&a=lca[i].lca;
if(mark[u]||mark[v]) continue;
modify(a);
ans++;
}
printf("%d\n",ans);
}
return ;
}
[HDU6203]ping ping ping的更多相关文章
- 死亡之ping(Ping of Death)
最简单的基于IP的攻击可能要数著名的死亡之ping,这种攻击主要是由于单个包的长度超过了IP协议规范所规定的包长度.产生这样的包很容易,事实上,许多操作系统都提供了称为ping的网络工具.在为Wind ...
- Windows防火墙开启ping,禁ping的配置
当我通过本机Ping另一台在同一局域网内(即在同一网段)的计算机时,发现,如果防火墙开启的话,无论如何也ping不通.一旦关闭防火墙就可以ping通了.这是为什么呢?究竟该怎么设置呢? 原因是这样的, ...
- Windows防火墙开启ping,禁ping的配置方法
Windows 7,Win 2008 R2,2012 R2: Windows防火墙 --> 高级设置 --> 入站规则 --> 在列表里找到“文件和打印机共享(回显请求 - ICMP ...
- 无法上外网, ping网关ping不通的解决——arp命令
转自:http://jingyan.baidu.com/article/3c48dd34873909e10be35894.html 转自:http://man.linuxde.net/arp 用来管理 ...
- Linux Centos7使用ping命令ping不通网络的解决方案
本解决方案不配置dns,都是ping的IP地址,所以如果想ping域名,则加上DNS项的配置后自行尝试吧 我使用的虚拟机系统信息: Linux:Centos7 Network:虚拟机设置的桥接模式(自 ...
- Linux下长时间ping网络加时间戳并记录到文本
Linux下长时间ping网络加时间戳并记录到文本 由于一些原因,比如需要检查网络之间是否存在掉包等问题,会长时间去ping一个地址,由于会输出大量的信息而且最好要有时间戳,因此我们可以使用简单的 ...
- DAY6 使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单
设置防火墙策略时,关于SSH:22访问权限,我们常常会设置服务器只接受某个固定IP(如公司IP)访问,但是当我们出差或在家情况需要登录服务器怎么办呢? 常用两种解决方案:1.通过VPN操作登录主机: ...
- 本机,同机房,同城,异地,不同城,腾讯云ping延时值
本机,同机房,同城,异地,不同城,腾讯云ping延时值 ping本机: 0.01ms ping同机房机器: 0.1ms ping同城机器: 1ms ping不同城机器: 20ms 北(南)方ping南 ...
- 使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单
设置防火墙策略时,关于SSH:22访问权限,我们常常会设置服务器只接受某个固定IP(如公司IP)访问,但是当我们出差或在家情况需要登录服务器怎么办呢? 常用两种解决方案:1.通过VPN操作登录主机: ...
- 003.同时Ping多个IP(select实现IO复用,信号计时),ping程序升级版
写这个的目的主要是为了以后的方便: 1.信号计时函数的使用 2.ip头的构建和icmp头的构建 3.selec函数t的用法 代码实现: /src/ping.h /* * ping.h * * Crea ...
随机推荐
- [转]STL 容器一些底层机制
1.vector 容器 vector 的数据安排以及操作方式,与 array 非常相似.两者的唯一区别在于空间的运用的灵活性.array 是静态空间,一旦配置了就不能改变,vector 是动态数组.在 ...
- go 数组
数组的定义和 初始化 数组是同一类型的元素集合 ]int //定义⼀个数组 Go中数组下标从0开始,因此长度为n的数组下标范围:[0,n-1] 整数数组中的元素默认初始化为0,字符串数组中的元素默认初 ...
- 使用netcat的正向 / 反向shell
reverse shell bind shell reverse shell描述图: 在此示例中,目标使用端口4444反向连接攻击主机.-e选项将Bash shell发回攻击主机.请注意,我们也可以在 ...
- 使用ubifs格式的根文件系统
配置内核,使其支持ubifs文件系统 1)Device Drivers --->Memory Technology Device (MTD) support --->UBI - Uns ...
- sicily 1231. The Embarrassed Cryptography
Time Limit: 2sec Memory Limit:32MB Description The young and very promising cryptographer Odd Ev ...
- js点击页面其他地方如何隐藏div元素菜单
web页面常用的一个需求,写下拉菜单是我们往往不是用select_option,而是自定义一个元素列出选项来满足需求,当我们点击按钮出现菜单, 点击按钮或菜单以外页面空白地方隐藏该菜单,这里提供一种简 ...
- android studio 解决avd启动问题 ----waiting for target device come online
android studio 模拟器打不开,一直停留在第三方.waiting for target device come online 问题解决方法 方法1.Android Emulator 未 ...
- pip3
pip3 install django #安装rabbitmq连接模块 pip3 install pika pip3 install paramiko pip3 install ipython pip ...
- Unix IPC之Posix消息队列(2)
/* Query status and attributes of message queue MQDES. */ extern int mq_getattr (mqd_t __mqdes, stru ...
- SQL Server 2000中查询表名,列名及字段类型
经常碰到一些忘记表名称的情况,此时只记得个大概,此时可通过查询系统表Sysobjects找到所要的表名,如要查找包含用户的表名,可通过以下SQL语句实现, Select * From sysobjec ...