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. Oracle"TNS监听程序找不到符合协议堆栈要求的可用处理程序"解决方案

    问题描述:在使用ETL工具通过odbc方式连接Oracle进行数据抽取的过程中,Oracle 监听日志报错如下: 根本原因就是Oracle的process和session已经达到了甚至超过了最大值,解 ...

  2. 暴雪的hash算法[翻译]

    原文来自:http://sfsrealm.hopto.org/inside_mopaq/chapter2.htm#hashes 促进历史进步的大多数契机都是在解决特定问题的过程中产生的,本文讨论一下M ...

  3. Photoshop扣除特定颜色背景

    步骤:打开ps--打开图片---选择--选择色彩范围---取样颜色(用吸管选定颜色)-- ( )再按delete键删除--点击文件--点击储存为PSD. 这样就抠除了特定范围色彩内的背景.

  4. QT+VTK 对接使用

    由于MFC和pcl的不兼容问题,只能用QT和VTK进行程序开发,确实是一件蛋疼的事! 出自于QT与VTK结合系列:http://blog.csdn.net/tonylk/article/details ...

  5. js去除html标记

    function ff(str) { var dd = str.replace(/<\/?.+?>/g, ""); var dds = dd.replace(/ /g, ...

  6. Android对手尽皆铩羽,鸿蒙如何绝地求生?

    Android对手尽皆铩羽,鸿蒙如何绝地求生? 作为华为绝地反击备胎计划中的重要组成部分,鸿蒙被国人寄予了厚望.但是,除了热情我们更应该理性关注,鸿蒙对决Android未来有几成胜算?还有哪些问题需要 ...

  7. SQL2012安装

    SQL2012安装 1.打开SQL安装包,点击setup安装 2.选择安装界面,点击全新安装 3.验证通过后,点击确定 4.选择我接受,点击下一步 5.在外网环境,可进行在线更新,内网环境取消勾选产品 ...

  8. BZOJ1143: [CTSC2008]祭祀river 网络流_Floyd_最大独立集

    Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都 会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组 ...

  9. 路飞学城Python-Day153

    Scrapy核心组件

  10. jmeter实现逻辑控制器