HDU 6203 ping ping ping(贪心+LCA+DFS序+BIT)
ping ping ping
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 207 Accepted Submission(s): 47
The structure of the computer room in Northeastern University is pretty miraculous. There are n servers, some servers connect to the gateway whose IP address is 0 directly. All servers are connected with each other by n netting twines. It is said that this structure is favorable for maintaining physical problem of servers.
But because of an unexpected rainstorm, the computer room was destroyed by a terrible thunderclap!
Our maintainer Bittersweet found that many servers were not able to be visited, so he hurried to the computer room to lookup the reason. After several hours, Bittersweet realized that some net gape of servers were broken by thunderclap. However, there were too many servers to find out all the broken net gapes quickly. So he came up with an idea to assess the damaged condition roughly. Bittersweet decided to turn on some servers and ping other servers randomly, then record the unsuccessful pairs of servers.
Now he need a program to analyze the record to confirm what is the minimum number of servers whose net gape was destroyed by thunderclap. Can you help him to complete this work?
There are at most 20 test cases.
In each test case, the first line is an integer n (3≤n≤104), denoting the number of servers. The IP address of these servers is 1…n.
Then follows n lines, each line contains two integers u and v (0≤u,v≤n), denoting that the server whose IP address is u is connected with the server whose IP address is v by netting twine initially.
After those, there is one line contains only an integer p (p≤50000), denoting the number that Bittersweet uses ping.
Then follows p lines, each line contains two integers U and V , denoting when using server U to ping server V, it returned unsuccessful.
题目链接:HDU 6203
题意就是求最少的点覆盖所有给定的q条路径,其实可以转换一下,就是求最多的路径集合使得集合内的路径两两之间不存在交点,那么跟HDU 4912是一样的题目了,先把所有路径按照其LCA深度从大到小排,即先处理靠下方的,因为这样对其他路径影响肯定是最小的,然后看这条路径的两端$u$和$v$是否被标记过,如果没标记就把以$LCA(u,v)$为根的子树全部标记上,否则就说明这条路径至少与其他路径存在一个交点(被标记的点)以此类推,如何快速标记子树呢?DFS序啊,而且子树是区间更新,查询端点是点查询,用BIT刚刚好
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 10010;
const int M = 50010;
struct edge
{
int to, nxt;
edge() {}
edge(int _to, int _nxt): to(_to), nxt(_nxt) {}
} E[N << 1];
int head[N], tot;
struct info
{
int u, v, lca, dep;
bool operator<(const info &rhs)const
{
return dep > rhs.dep;
}
} node[M];
int D[N << 1], F[N], ver[N << 1], dp[N << 1][19], ts, dep[N];
int T[N], L[N], R[N], idx; void init()
{
CLR(head, -1);
tot = 0;
ts = 0;
CLR(T, 0);
idx = 0;
}
inline void add(int s, int t)
{
E[tot] = edge(t, head[s]);
head[s] = tot++;
}
void dfs(int u, int f, int d)
{
ver[++ts] = u;
D[ts] = d;
F[u] = ts; L[u] = ++idx;
dep[u] = d;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (v != f)
{
dfs(v, u, d + 1);
ver[++ts] = u;
D[ts] = d;
}
}
R[u] = idx;
}
void RMQ_init(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
dp[i][0] = i;
for (j = 1; l + (1 << j) - 1 <= r; ++j)
{
for (i = l; i + (1 << j) - 1 <= r; ++i)
{
int a = dp[i][j - 1], b = dp[i + (1 << (j - 1))][j - 1];
dp[i][j] = D[a] < D[b] ? a : b;
}
}
}
int LCA(int u, int v)
{
int l = F[u], r = F[v];
if (l > r)
swap(l, r);
int len = r - l + 1;
int k = 0;
while (1 << (k + 1) <= len)
++k;
int a = dp[l][k], b = dp[r - (1 << k) + 1][k];
return D[a] < D[b] ? ver[a] : ver[b];
}
namespace BIT
{
void add(int k, int v)
{
while (k < N)
{
T[k] += v;
k += (k & -k);
}
}
int sum(int k)
{
int ret = 0;
while (k)
{
ret += T[k];
k -= (k & -k);
}
return ret;
}
}
int main(void)
{
int n, u, v, i, q;
while (~scanf("%d", &n))
{
init();
for (i = 0; i < n; ++i)
{
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(0, -1, 0);
RMQ_init(1, ts);
scanf("%d", &q);
for (i = 0; i < q; ++i)
{
scanf("%d%d", &node[i].u, &node[i].v);
node[i].lca = LCA(node[i].u, node[i].v);
node[i].dep = dep[node[i].lca];
}
sort(node, node + q);
int ans = 0;
for (i = 0; i < q; ++i)
{
if (BIT::sum(L[node[i].u]) || BIT::sum(L[node[i].v]))
continue;
BIT::add(L[node[i].lca], 1);
BIT::add(R[node[i].lca] + 1, -1);
++ans;
}
printf("%d\n", ans);
}
return 0;
}
HDU 6203 ping ping ping(贪心+LCA+DFS序+BIT)的更多相关文章
- 【BZOJ】2819: Nim(树链剖分 / lca+dfs序+树状数组)
题目 传送门:QWQ 分析 先敲了个树链剖分,发现无法AC(其实是自己弱,懒得debug.手写栈) 然后去学了学正解 核心挺好理解的,$ query(a) $是$ a $到根的异或和. 答案就是$ l ...
- 【bzoj1146】[CTSC2008]网络管理Network 倍增LCA+dfs序+树状数组+主席树
题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...
- HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- bzoj3991 lca+dfs序应用+set综合应用
/* 给定一棵树,树上会出现宝物,也会有宝物消失 规定如果要收集树上所有宝物,就要选择一个点开始,到每个宝物点都跑一次,然后再回到那个点 现在给定m次修改,每次修改后树上就有一个宝物消失,或者一个宝物 ...
- BZOJ3881[Coci2015]Divljak——AC自动机+树状数组+LCA+dfs序+树链的并
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- nowcoder172C 保护 (倍增lca+dfs序+主席树)
https://www.nowcoder.com/acm/contest/172/C (sbw大佬太强啦 orz) 先把每一个路径(x,y)分成(x,lca),(y,lca)两个路径,然后就能发现,对 ...
随机推荐
- python之打印九九乘法表
配置环境:python 3.6 python编辑器:pycharm 整理成代码如下: #!/usr/bin/env python #-*- coding: utf-8 -*- #九九乘法表 #分析:九 ...
- 002---tcp/ip五层详解
tcp/ip 五层模型讲解 越靠底层就越接近硬件,越靠上层越接近用户.先从底层看起,理解整个互联网通信的原理. 物理层(传输电信号) 孤立的计算机想要一起玩.就必须用硬件在计算机之间完成组网.以硬件做 ...
- Java线程和多线程(十)——TimerTask
Java中的java.util.Timer是一个工具类,可以用于调度一个线程在将来的某一个时刻执行特定的任务.Java Timer类可以将一个任务定时执行一次,或者是以后以每隔一定的时间间隔来触发一次 ...
- 8 TFTP代码详解 协议写在程序中
1.版本1:发送请求 # -*- coding:utf-8 -*- import struct from socket import * #0. 获取要下载的文件名字: downloadFileNam ...
- 2002: [Hnoi2010]Bounce 弹飞绵羊
2002: [Hnoi2010]Bounce 弹飞绵羊 https://www.lydsy.com/JudgeOnline/problem.php?id=2002 分析: 绵羊在弹飞的路径中相当于一棵 ...
- 从底层带你理解Python中的一些内部机制
下面博文将带你创建一个字节码级别的追踪API以追踪Python的一些内部机制,比如类似YIELDVALUE.YIELDFROM操作码的实现,推式构造列表(List Comprehensions).生成 ...
- oracle 数据库字段名与实体类字段名称不匹配的处理方法
之前公司一直都使用sql server 即使数据库字段名称与实体类名称不相同 可以使用诸如: select id as userId from tb_user 这种写法,可换到了oracle 之后坑爹 ...
- 梳理 Opengl ES 3.0 (一)宏观着眼
Opengl ES 可以理解为是在嵌入式设备上工作的一层用于处理图形显示的软件,是Opengl 的缩水版本. 下图是它的工作流程示意图: 注意图中手机左边的EGL Layer Opengl ES是跨平 ...
- Visual Studio 6.0安装包
点击下载
- POJ 2168 Joke with Turtles(DP)
Description There is a famous joke-riddle for children: Three turtles are crawling along a road. One ...