当前位置:网站首页>hdu 4912 Paths on the tree(lca+馋)
hdu 4912 Paths on the tree(lca+馋)
2022-07-06 13:44:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
意甲冠军:它使树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;
}版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117044.html原文链接:https://javaforall.cn
边栏推荐
- High precision face recognition based on insightface, which can directly benchmark hongruan
- 在Pi和Jetson nano上运行深度网络,程序被Killed
- 缓存更新策略概览(Caching Strategies Overview)
- Earned value management EVM detailed explanation and application, example explanation
- 新入职一家公司需要去实践和注意的内容
- Leveldb source code analysis series - main process
- npm run dev启动项目报错 document is not defined
- [go][转载]vscode配置完go跑个helloworld例子
- 【力扣刷题】一维动态规划记录(53零钱兑换、300最长递增子序列、53最大子数组和)
- JPEG2000 matlab source code implementation
猜你喜欢

Checkpoint of RDD in spark

Why is the cluster mode of spark on Yan better than the client mode

Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)
![[asp.net core] set the format of Web API response data -- formatfilter feature](/img/95/b7e7b5e9e9ac1d9295c17640beccb3.jpg)
[asp.net core] set the format of Web API response data -- formatfilter feature

PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology

Unity3D学习笔记6——GPU实例化(1)
![[Digital IC manual tearing code] Verilog automatic beverage machine | topic | principle | design | simulation](/img/75/c0656c4890795bd65874b4f2b16462.jpg)
[Digital IC manual tearing code] Verilog automatic beverage machine | topic | principle | design | simulation

Shake Sound poussera l'application indépendante de plantation d'herbe "louable", les octets ne peuvent pas oublier le petit livre rouge?

中国白酒的5场大战

美国科技行业结束黄金时代,芯片求售、裁员3万等哀声不断
随机推荐
b站视频链接快速获取
用aardio写一个旋转验证码标注小工具
JS method to stop foreach
Michael smashed the minority milk sign
[asp.net core] set the format of Web API response data -- formatfilter feature
在Pi和Jetson nano上运行深度网络,程序被Killed
Leveldb source code analysis series - main process
爬虫实战(五):爬豆瓣top250
numpy 下载安装
[daily] win10 system setting computer never sleeps
1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
[Chongqing Guangdong education] Information Literacy of Sichuan Normal University: a new engine for efficiency improvement and lifelong learning reference materials
It's not my boast. You haven't used this fairy idea plug-in!
Basic introduction of figure
PostgreSQL 修改数据库用户的密码
50个常用的Numpy函数解释,参数和使用示例
From campus to Tencent work for a year of those stumbles!
Persistence / caching of RDD in spark
The underlying implementation of string
十一、服务介绍及端口