Paths on the tree

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 297    Accepted Submission(s): 93

Problem Description
bobo has a tree, whose vertices are conveniently labeled by 1,2,…,n.



There are m paths on the tree. bobo would like to pick some paths while any two paths do not share common vertices.



Find the maximum number of paths bobo can pick.
 
Input
The input consists of several tests. For each tests:



The first line contains n,m (1≤n,m≤105). Each of the following (n - 1) lines contain 2 integers ai,bi denoting an edge between vertices ai and bi (1≤ai,bi≤n). Each of the following
m lines contain 2 integers ui,vi denoting a path between vertices ui and vi (1≤ui,vi≤n).
 
Output
For each tests:



A single integer, the maximum number of paths.
 
Sample Input
3 2
1 2
1 3
1 2
1 3
7 3
1 2
1 3
2 4
2 5
3 6
3 7
2 3
4 5
6 7
 
Sample Output
1
2
 
Author
Xiaoxu Guo (ftiasch)

一棵树,给出m条路径,然后问最多选择多少条路径,使得每两条路径之间没有 不论什么公共点,公共边。

把m条路径求出lca,然后依照lca深度从大到小排序,把路径上经过的点标记出来,贪心就可以。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100010;
int head[maxn],tol,fa[maxn][20],dep[maxn];
struct Edge{
int next,to;
Edge(int _next=0,int _to=0){
next=_next;to=_to;
}
}edge[10*maxn];
void addedge(int u,int v){
edge[tol]=Edge(head[u],v);
head[u]=tol++;
}
void bfs(int s){
queue<int> q;
dep[s]=0,fa[s][0]=s;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=1;i<20;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u][0])continue;
fa[v][0]=u;
dep[v]=dep[u]+1;
q.push(v);
}
}
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=19;i>=0;i--)if((1<<i)&(dep[x]-dep[y]))x=fa[x][i];
if(x==y)return x;
for(int i=19;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
struct QQ{
int u,v,lca;
}pp[100100];
bool vis[100100];
int n,m;
bool cmp(QQ a,QQ b){
return dep[a.lca]>dep[b.lca];
}
int main()
{
while(~scanf("%d%d",&n,&m)){
memset(head,-1,sizeof(head));
tol=0;
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
bfs(1);
for(int i=0;i<m;i++){
scanf("%d%d",&pp[i].u,&pp[i].v);
pp[i].lca=LCA(pp[i].u,pp[i].v);
}
sort(pp,pp+m,cmp);
memset(vis,0,sizeof(vis));
int ans=0;
for(int i=0;i<m;i++){
int lca=pp[i].lca,flag=1,u=pp[i].u,v=pp[i].v;
if(vis[u]||vis[v]||vis[lca])continue;
for(int j=u;j!=lca;j=fa[j][0])
if(vis[j]){
flag=0;break;
}
for(int j=v;j!=lca;j=fa[j][0])
if(vis[j]){
flag=0;break;
}
if(!flag)continue;
ans++;
vis[lca]=1;
for(int j=u;j!=lca;j=fa[j][0])vis[j]=1;
for(int j=v;j!=lca;j=fa[j][0])vis[j]=1;
}
printf("%d\n",ans);
}
return 0;
}

HDU 4912 lca贪心的更多相关文章

  1. HDU 4912 LCA + 贪心

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

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

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

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

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

  4. Hdu 4864(Task 贪心)(Java实现)

    Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...

  5. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

  6. hdu4912 LCA+贪心

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

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

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

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

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

  9. hdu 2037简单贪心--活动安排问题

    活动安排问题就是要在所给的活动集合中选出最大的相容活动子集合,是可以用贪心算法有效求解的很好例子.该问题要求高效地安排一系列争用某一公共资源的活动.贪心算法提供了一个简单.漂亮的方法使得尽可能多的活动 ...

随机推荐

  1. 使用原生JS实现简单的ajax

    Ajax是一种使用javascript内置对象向服务器发送请求/接收响应的技术.它可以再页面已经完全显示出来之后再和服务器进行少量的数据交互,所以可以实现局部内容的刷新. ajax的实现,主要是靠ja ...

  2. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

  3. 找回消失的ubuntu启动选项

    启动菜单消失不是第一次了,这一次还是记录下来吧 原文链接:http://blog.chinaunix.net/uid-26527046-id-3748986.html 在安装windows后安装的ub ...

  4. SVN冲突出现原因及解决方法浅谈

    缘由 很简单,用svn合base,出现了各种各样奇怪的问题,虽然最终没有造成什么大的线上问题,但过程也是曲折的,耗费个人精力,也占用他人资源,不好不好,一点都不佛系. 究其原因,还是对为什么出现各种冲 ...

  5. JS面向对象(2)——原型链

    原型链用于ECMAScript的继承.其思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.说人话,我们知道,一个构造函数Subtype,其原型对象有一个指向构造函数的指针,这是联系构造函数和 ...

  6. java操作Excel的poi的简介

    一.POI概述 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 结构: HSSF - 提供读写Mi ...

  7. 5G开启百亿“机器人”时代 安全与标准仍待统一

    5G开启百亿“机器人”时代 安全与标准仍待统一 5G将会给物联网产业带来滚雪球效应,这将是数以百亿计“机器人”之间的网络. 在5G的助推下,物联网应用正在加速落地. 6月12日,CES Asia 20 ...

  8. iOS runLoop 原理多线程 总结 NSTimer优化

    可以理解为字面意思:Run 表示运行,Loop 表示循环.结合在一起就是运行的循环的意思.哈哈,我更愿意翻译为『跑圈』.直观理解就像是不停的跑圈. RunLoop 实际上是一个对象,这个对象在循环中用 ...

  9. iOS 小知识

    iOS 各版本: http://www.pig66.com/2018/145_1021/17357553.html

  10. C#学习 第十节

    操作符(operator) 1.操作符的概览 从上到下优先级依次减弱: 2.操作符的本质 操作符的本质是函数的简记法: 计算机的操作符不能脱离与它关联的数据类型: 3.操作符的优先级 可以使用括号 4 ...