HDU 4912 LCA + 贪心
说一下为什么按LCA深度从深到浅贪心是对的。我们可以直观感受一下,一条的路径会影响以这个lca为根的这颗树中的链,而深度越深,影响范围越小,所以先选影响范围小的路径。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int head[maxn], Next[maxn * 2], ver[maxn * 2], tot, f[maxn][20];
int deep[maxn], t;
struct Edge {
int u, v, lca, lca_deep;
bool operator < (const Edge& rhs) const {
return lca_deep > rhs.lca_deep;
}
};
Edge a[maxn];
bool v[maxn];
void add(int x, int y) {
ver[++tot] = y;
Next[tot] = head[x];
head[x] = tot;
}
queue<int> q;
void bfs() {
deep[1] = 1;
q.push(1);
while(!q.empty()) {
int x = q.front();
q.pop();
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(deep[y]) continue;
deep[y] = deep[x] + 1;
f[y][0] = x;
for (int j = 1; j <= t; j++)
f[y][j] = f[f[y][j - 1]][j - 1];
q.push(y);
}
}
} int query(int x, int y) {
if(deep[x] > deep[y]) swap(x, y);
for (int i = t; i >= 0; i--) {
if(deep[f[y][i]] >= deep[x]) y = f[y][i];
}
if(x == y) return x;
for (int i = t; i >= 0; i--)
if(f[x][i] != f[y][i]) {
x = f[x][i], y = f[y][i];
}
return f[x][0];
} void dfs(int x, int fa) {
if(v[x]) return;
v[x] = 1;
for (int i = head[x]; i; i = Next[i]) {
int y = ver[i];
if(y == fa) continue;
dfs(y, x);
}
} int main() {
int n, m, x, y;
while(~scanf("%d%d", &n, &m)) {
t = (int)(log(n) / log(2)) + 1;
tot = 0;
for (int i = 1; i <= n; i++) {
head[i] = 0;
v[i] = 0;
for (int j = 0; j <= t; j++)
f[i][j] = 0;
deep[i] = 0;
}
// memset(head, 0, sizeof(head));
// memset(f, 0, sizeof(f));
// memset(v, 0, sizeof(v));
// memset(deep, 0, sizeof(deep));
// tot = 0;
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
bfs();
int ans = 0;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a[i].u, &a[i].v);
a[i].lca = query(a[i].u, a[i].v);
a[i].lca_deep = deep[a[i].lca];
}
sort(a + 1, a + 1 + m);
for (int i = 1; i <= m; i++) {
if(!v[a[i].u] && !v[a[i].v]) {
ans++;
dfs(a[i].lca, f[a[i].lca][0]);
}
}
printf("%d\n", ans);
}
}
HDU 4912 LCA + 贪心的更多相关文章
- HDU 4912 lca贪心
Paths on the tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 2586 + HDU 4912 最近公共祖先
先给个LCA模板 HDU 1330(LCA模板) #include <cstdio> #include <cstring> #define N 40005 struct Edg ...
- hdu 4912 Paths on the tree(树链拆分+贪婪)
题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...
- Hdu 4864(Task 贪心)(Java实现)
Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- hdu4912 LCA+贪心
题意: 给你一棵树和m条边,问你在这些边里面最多能够挑出多少条边,使得这些边之间不能相互交叉. 思路: lca+贪心,首先对于给的每个条边,我们用lca求出他们的公共节点,然后在 ...
- HDU 4912 Paths on the tree(LCA+贪心)
题目链接 Paths on the tree 来源 2014 多校联合训练第5场 Problem B 题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目. 我们先求出每一条路径 ...
- hdu 4912 Paths on the tree(lca+馋)
意甲冠军:它使树m路径,当被问及选择尽可能多的路径,而这些路径不相交. 思考:贪心,比較忧伤.首先求一下每对路径的lca.依照lca的层数排序.在深一层的优先级高.那么就能够贪心了,每次选择层数最深的 ...
- hdu 2037简单贪心--活动安排问题
活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...
随机推荐
- Switching from Redhat Linux to Oracle Linux in about 5,000 easy steps
Wayback When I remember being at Oracle Open World when Larry Ellison unveiled Oracle Enterprise Lin ...
- Project://CRM
初始化 考勤 录入成绩 查看成绩 待续...
- Leetcode 976. Largest Perimeter Triangle
送分题 class Solution(object): def largestPerimeter(self, A): """ :type A: List[int] :rt ...
- HttpServletRequest获取请求得URL信息
request对象中包含的是请求信息,当我们在浏览器地址栏上输入:http://localhost:8080/Example/AServlet?username=zhangsan,这段地址也会作为请求 ...
- Laser
Petya is the most responsible worker in the Research Institute. So he was asked to make a very impor ...
- poj2374 Fence Obstacle Course[线段树+DP]
https://vjudge.net/problem/POJ-2374 吐槽.在这题上面磕了许久..英文不好题面读错了qwq,写了个错的算法搞了很久..A掉之后瞥了一眼众多julao题解,**,怎么想 ...
- [ Laravel 5.5 文档 ] 数据库操作 —— 在 Laravel 中轻松实现分页功能
 简介 在其他框架中,分页是件非常痛苦的事,Laravel 让这件事变得简单易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基于 ...
- Yii 查询 搜索
一.视图 <div class="form-horizontal"> <?php $form = $this->beginWidget('CActiveFo ...
- controller返回js中文变成?解决方案
在使用spring-mvc的mvc的时候既享受它带来的便捷,又头痛它的一些问题,比如经典的中文乱码问题.现在是用json作为客户端和服务端 的数据交换格式貌似很流行,但是在springmvc中有时候会 ...
- Ubuntu下部署GitLab-——基于14.04系统
搭建GitLab的目的: 方便公司开发管理代码 GitLab实现的功能: 1.关闭了gitlab的注册功能 2.修改了默认端口 3.汉化 0.前期准备 # 环境 Ubuntu 14.04 root@i ...