题目链接:hdu 4912 Paths on the tree

题目大意:给定一棵树,和若干个通道。要求尽量选出多的通道,而且两两通道不想交。

解题思路:用树链剖分求LCA,然后依据通道两端节点的LCA深度排序,从深度最大优先选。推断两个节点均没被标

记即为可选通道。

每次选完通道。将该通道LCA下面点所有标记。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 1e5 + 5; int N, M, E, first[maxn], jump[maxn * 2], link[maxn * 2], vis[maxn];
int id, idx[maxn], top[maxn], far[maxn], son[maxn], cnt[maxn], dep[maxn]; inline void add_Edge (int u, int v) {
link[E] = v;
jump[E] = first[u];
first[u] = E++;
} void dfs (int u, int pre, int d) {
far[u] = pre;
son[u] = 0;
cnt[u] = 1;
dep[u] = d;
for (int i = first[u]; i + 1; i = jump[i]) {
int v = link[i];
if (v == pre)
continue;
dfs(v, u, d + 1);
cnt[u] += cnt[v];
if (cnt[son[u]] < cnt[v])
son[u] = v;
}
} void dfs (int u, int rot) {
top[u] = rot;
idx[u] = ++id;
if (son[u])
dfs(son[u], rot);
for (int i = first[u]; i + 1; i = jump[i]) {
int v = link[i];
if (v == far[u] || v == son[u])
continue;
dfs(v, v);
}
} void dfs (int u) {
if (vis[u])
return;
vis[u] = 1;
for (int i = first[u]; i + 1; i = jump[i]) {
int v = link[i];
if (v == far[u])
continue;
dfs(v);
}
} struct query {
int u, v, r, d;
void set(int u, int v, int r, int d) {
this->u = u;
this->v = v;
this->r = r;
this->d = d;
}
friend bool operator < (const query& a, const query& b) {
return a.d > b.d;
}
}q[maxn]; int LCA (int u, int v) {
int p = top[u], q = top[v];
while (p != q) {
if (dep[p] < dep[q]) {
swap(p, q);
swap(u, v);
}
u = far[p];
p = top[u];
}
return dep[u] < dep[v] ? u : v;
} void init () {
E = id = 0;
memset(first, -1, sizeof(first)); int u, v;
for (int i = 1; i < N; i++) {
scanf("%d%d", &u, &v);
add_Edge(u, v);
add_Edge(v, u);
}
dfs(1, 0, 0);
dfs(1, 1); for (int i = 0; i < M; i++) {
scanf("%d%d", &u, &v);
int rot = LCA(u, v);
q[i].set(u, v, rot, dep[rot]);
}
sort(q, q + M);
} int main () {
while (scanf("%d%d", &N, &M) == 2) {
init(); int ans = 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < M; i++) {
if (vis[q[i].u] || vis[q[i].v])
continue;
dfs(q[i].r);
ans++;
}
printf("%d\n", ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

hdu 4912 Paths on the tree(树链拆分+贪婪)的更多相关文章

  1. hdu5044 Tree 树链拆分,点细分,刚,非递归版本

    hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...

  2. HDU 4912 Paths on the tree(LCA+贪心)

    题目链接 Paths on the tree 来源  2014 多校联合训练第5场 Problem B 题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目. 我们先求出每一条路径 ...

  3. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  4. hdu 4912 Paths on the tree(lca+馋)

    意甲冠军:它使树m路径,当被问及选择尽可能多的路径,而这些路径不相交. 思考:贪心,比較忧伤.首先求一下每对路径的lca.依照lca的层数排序.在深一层的优先级高.那么就能够贪心了,每次选择层数最深的 ...

  5. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  6. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  7. HDU 5614 Baby Ming and Matrix tree 树链剖分

    题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...

  8. HDU 5044 Tree --树链剖分

    题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...

  9. HDU 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 Problem Description There is an old country and ...

随机推荐

  1. poj 3265 Problem Solving dp

    这个题目容易让人误以为是贪心就可以解决了,但是细想一下很容易举出反例. dp[i][j]表示解决了i个问题,最后一个月解决的问题数目. #include <iostream> #inclu ...

  2. java-IO操作性能对照

    在软件系统中.IO速度比内存速度慢,IO读写在非常多情况下会是系统的瓶颈. 在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作.以字节为处理单位:Reade ...

  3. css中padding中样式的顺序含义

    4种可能的情况.举例说明: padding:10px; 四个内边距都是10px padding:5px 10px; 上下5px 左右10px padding:5px 10px 15px; 上5px 右 ...

  4. NodeJS - Express4.0错误:Cannot read property &amp;#39;Store&amp;#39; of undefined

    Express在使用mongodb的时候app配置出错 //settings.js module.exports={ cookieSecret:"xxxx", db:"d ...

  5. Mybatis数据操作

    Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作   详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) M ...

  6. 玩转web之javaScript(五)---js和jquery一些不可不知的方法(input篇)

    很多时候我们都利用js和jquery中操作input,比如追加属性,改变属性值等等,我在这里简单的整理了一下,并在以后逐步补充. 1:删除input的某一属性. <input name=&quo ...

  7. Java Drp项目实战——Drp知多少

    是什么 Drp是Distribution Resource Planning的缩写,意思是分销资源计划.它是用来管理企业的执行于Internet上的分销网络的系统,是以商业流程优化为基础,它的核心是销 ...

  8. Linux System Programming note 8 ——File and Directory Management

    1. The Stat Family #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> ...

  9. 使用CSS如何悬停背景颜色变色 onmouseover、onmouseout

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. js匀速运动停止条件

    匀速运动,怎么让它到达指定位置时停止呢? 原理: 1,物体和目标的差值距离小于等于速度时,即停止 2,接着让物体移动位置等于目标位置 示例:匀速运动停止 html部分 <input type=& ...