题目链接 Paths on the tree

来源  2014 多校联合训练第5场 Problem B

题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目。

我们先求出每一条路径(u, v)中u和v的LCA:w,按照路径的w的深度大小deep[w]对所有的路径排序。

deep[w]越大,排在越前面。

然后从第一条路径开始一次处理,看c[u]和c[v]是否都没被标记过,如果都没被标记过则我们把这条路径选上,把答案加1。

同时标记以w为根的子树的节点为1,方便后续对c数组的查询。

时间复杂度$O(mlogn + mlogm + n)$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 1e5 + 10;
const int A = 24; int f[N][A];
int n, m, ans;
int deep[N], c[N];
vector <int> v[N]; struct node{
int x, y, z;
friend bool operator < (const node &a, const node &b){
return deep[a.z] > deep[b.z];
}
} p[N]; void dfs(int x, int fa, int dep){
deep[x] = dep;
if (fa){
f[x][0] = fa;
for (int i = 0; f[f[x][i]][i]; ++i) f[x][i + 1] = f[f[x][i]][i];
}
for (auto u : v[x]){
if (u == fa) continue;
dfs(u, x, dep + 1);
}
} int LCA(int a, int b){
if (deep[a] < deep[b]) swap(a, b);
for (int i = 0, delta = deep[a] - deep[b]; delta; delta >>= 1, ++i) if (delta & 1) a = f[a][i];
if (a == b) return a;
dec(i, 19, 0) if (f[a][i] != f[b][i]) a = f[a][i], b = f[b][i];
return f[a][0];
} void tag(int x){
c[x] = 1;
for (auto u : v[x]){
if (u == f[x][0]) continue;
if (c[u] == 0) tag(u);
}
} int main(){ while (~scanf("%d%d", &n, &m)){
memset(f, 0, sizeof f);
rep(i, 0, n + 1) v[i].clear();
rep(i, 2, n){
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
} memset(deep, 0, sizeof deep);
dfs(1, 0, 0);
ans = 0; rep(i, 1, m){
int x, y;
scanf("%d%d", &x, &y);
int z = LCA(x, y);
p[i] = {x, y, z};
} sort(p + 1, p + m + 1); memset(c, 0, sizeof c);
rep(i, 1, m){
int u = p[i].x, w = p[i].y;
if (c[u] == 0 && c[w] == 0){
tag(p[i].z);
++ans;
}
} printf("%d\n", ans); }
return 0;
}

HDU 4912 Paths on the tree(LCA+贪心)的更多相关文章

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

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

  2. hdu 4912 Paths on the tree(树链拆分+贪婪)

    题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...

  3. ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...

  4. HDU 2586 + HDU 4912 最近公共祖先

    先给个LCA模板 HDU 1330(LCA模板) #include <cstdio> #include <cstring> #define N 40005 struct Edg ...

  5. HDU 4912 lca贪心

    Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  7. codeforce F - Three Paths on a Tree

    F. Three Paths on a Tree time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. hdu4912 LCA+贪心

    题意:       给你一棵树和m条边,问你在这些边里面最多能够挑出多少条边,使得这些边之间不能相互交叉. 思路:      lca+贪心,首先对于给的每个条边,我们用lca求出他们的公共节点,然后在 ...

  9. HDU 4912 LCA + 贪心

    题意及思路 说一下为什么按LCA深度从深到浅贪心是对的.我们可以直观感受一下,一条的路径会影响以这个lca为根的这颗树中的链,而深度越深,影响范围越小,所以先选影响范围小的路径. #include & ...

随机推荐

  1. Java-basic-4-数据类型

    Number类 装箱:将内置数据类型作为包装类对象使用:拆箱:相反 public class test{ public static void main(String args[]) { // box ...

  2. graph-SCC

    strongly connected component(SCC): 里面的任一对顶点都是互相可达的. 一个有向图,将每个SCC缩成一个点,那么这个图就变成了DAG(有向无环图). 原图进行DFS之后 ...

  3. SDUSToj第十一次作业源代码格式问题

    Problem I: 源代码的格式 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 1471  Solved: 634 [Submit][Status][W ...

  4. HDU 3435 KM A new Graph Game

    和HDU 3488一样的,只不过要判断一下是否有解. #include <iostream> #include <cstdio> #include <cstring> ...

  5. jQuery ui 百叶窗blind方向设置

    百叶窗特效(Blind Effect)参数direction设置 $( document ).click(function() { $( "#toggle" ).toggle( & ...

  6. Django Model two

    Django_model: eg: class XXXX(models.Model): nid = models.AutoField(primary_Key=True) name = models.C ...

  7. Scala学习-01-变量与类型

    Scala运行在jvm之上,可以调用Java类库和与Java框架交互,并将面向对象与面向函数结合在一起. 特点: 1 保留了静态类型检查.安全保障高. 2 函数式编程,更加灵活. 3 运行于jvm之上 ...

  8. set的特性和基本用法——python3.6

    特性 无序,不重复的数据组合,用{}表示,eg:{1,2,3,4,5,6} 用途 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之间的交集,差集,并集,对称差集,包含(子集和超集,相交 ...

  9. Install Oracle 11G Release 2 (11.2) on Centos Linux 7

    Install Oracle 11G Release 2 (11.2) on Centos Linux 7 This article presents how to install Oracle 11 ...

  10. pip安装及使用

    1.pip下载安装 1.1 pip下载 # wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5= ...