意甲冠军:它使树m路径,当被问及选择尽可能多的路径,而这些路径不相交。

思考:贪心,比較忧伤。首先求一下每对路径的lca。依照lca的层数排序。在深一层的优先级高。那么就能够贪心了,每次选择层数最深的那一个,假设两个端点没有被标记,那么就选择这条路径,把lca的子树都标记掉。

代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
struct Edge
{
int v,next;
Edge(int v=0,int next=0):v(v),next(next){}
}edges[maxn<<1];
struct Node
{
int u,v,id;
Node(int u=0,int v=0,int id=0):u(u),v(v),id(id) {}
}node[maxn];
int head[maxn],nEdge;
int LCA[maxn],pa[maxn],d[maxn];
bool vis[maxn];
vector<pair<int,int> >querys[maxn];
int Find(int x)
{
return x==pa[x]?x:pa[x]=Find(pa[x]);
}
void AddEdges(int u,int v)
{
edges[++nEdge]=Edge(v,head[u]);
head[u]=nEdge;
edges[++nEdge]=Edge(u,head[v]);
head[v]=nEdge;
}
void dfs(int u,int fa)
{
pa[u]=u;
pair<int,int>pii;
for(int i=0;i<(int)querys[u].size();++i)
{
pii=querys[u][i];
if(d[pii.first]==-1) continue;
LCA[pii.second]=Find(pii.first);
}
for(int k=head[u];k!=-1;k=edges[k].next)
{
int v=edges[k].v;
if(v==fa) continue;
d[v]=d[u]+1;
dfs(v,u);
pa[v]=u;
}
}
bool cmp(Node a,Node b)
{
int u=LCA[a.id];
int v=LCA[b.id];
return d[u]>d[v];
}
void Init(int n)
{
memset(vis,0,sizeof(vis));
memset(head,0xff,sizeof(head));
nEdge=-1;
memset(d,0xff,sizeof(d));
for(int i=0;i<=n;++i)
querys[i].clear();
}
void tour(int u)
{
vis[u]=true;
for(int k=head[u];k!=-1;k=edges[k].next)
{
int v=edges[k].v;
if(vis[v]||d[v]<d[u]) continue;
tour(v);
}
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,m;
while(~scanf("%d%d",&n,&m))
{
Init(n);
int u,v;
for(int i=1;i<n;++i)
{
scanf("%d%d",&u,&v);
AddEdges(u,v);
}
for(int i=0;i<m;++i)
{
scanf("%d%d",&u,&v);
querys[u].push_back(make_pair(v,i));
querys[v].push_back(make_pair(u,i));
node[i].u=u;node[i].v=v;
node[i].id=i;
}
d[1]=0;
dfs(1,-1);
sort(node,node+m,cmp);
int cnt=0;
for(int i=0;i<m;++i)
{
if(!vis[node[i].u]&&!vis[node[i].v])
{
cnt++;
tour(LCA[node[i].id]);
}
}
printf("%d\n",cnt);
}
return 0;
}

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

hdu 4912 Paths on the tree(lca+馋)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 4912 lca贪心

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

  6. 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 ...

  7. hdu Dylans loves tree [LCA] (树链剖分)

    Dylans loves tree view code#pragma comment(linker, "/STACK:1024000000,1024000000") #includ ...

  8. HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树

    lca的做法还是非常明显的.简单粗暴, 只是不是正解.假设树是长链就会跪,直接变成O(n).. 最后跑的也挺快,出题人还是挺阳光的.. 动态树的解法也是听别人说能ac的.预计就是放在splay上剖分一 ...

  9. HDU 4912 LCA + 贪心

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

随机推荐

  1. 一个简单而经典的RTX51 Tiny应用实例

    关于RTX51 Tiny嵌入式实时操作系统的描写叙述请參考本人的上一篇博文(RTX51 Tiny实时操作系统学习笔记-初识RTX51 Tiny). 本篇博文.我将通过一个实例代码,带大家深入了解一下R ...

  2. js正则匹配html内容

    1.得到网页上的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"" ...

  3. C#操作注册表--DarrenF

    步骤/方法 读取指定名称的注册表的值 private string GetRegistData(string name) { string registData; RegistryKey hkml = ...

  4. Windows Phone开发(27):隔离存储A

    原文:Windows Phone开发(27):隔离存储A 在很多资料或书籍上都翻译为"独立存储",不过,我想了一下,决定将IsolatedStorage翻译为"隔离存储& ...

  5. Drupal 7 模 .info 文件描述

    Drupal使用.info文件以节省话题(theme)和模块(modules)基本数据(metadata). 编码:推荐使用UTF-8.这是一个没有BOM(Byte Order Mark). 下面是一 ...

  6. WCF配置文件

    因为要上传较大的图片,WCF传递数组的默认的最大数组16KB就不够了.以下讲解配置内容. 服务端配置 这里一个WCF项目中有1个服务,配置文件如下(位于system.serviceModel标签中): ...

  7. 开源Math.NET基础数学类库使用(17)C#计算矩阵条件数

    原文:[原创]开源Math.NET基础数学类库使用(17)C#计算矩阵条件数                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...

  8. Ubuntu 12.04开启3D桌面特效

    1.设定软件源,更新软件 点击左边栏Dash主页(Ubuntu图标),输入更新管理器,会出现更新管理器,打开后点设置,弹出软件源对话框,为确保能够正常更新,选主服务器 点击检查,更新完后,点重启 2. ...

  9. Unreal Engine 4 创建Destructible Mesh(可破坏网格)

    Unreal Engine 4的物理引擎用的是PhysX. 支持网格破坏.布料.物理粒子等,非常强大.曾经须要编码才干完毕的工作,在Unreal Engine 4 中仅仅须要拖拖拽拽就完毕了,非常方便 ...

  10. 【原创翻译】认识MVC设计模式:web应用开发的基础(实际编码篇)

    原文地址:http://www.larryullman.com/2009/10/15/understanding-mvc-part-3/ 全系列INDEX [原创翻译]认识MVC设计模式:web应用开 ...