HDU 2586 + HDU 4912 最近公共祖先
先给个LCA模板
HDU 1330(LCA模板)
#include <cstdio>
#include <cstring>
#define N 40005
struct Edge{
int x,y,d,ne;
};
Edge e[N*],e2[N*];
int be[N],be2[N],all,all2,n,m;
bool vis[N];
int fa[N];
int ancestor[N][];
int dis[N]; void add(int x, int y, int d, Edge e[], int be[], int &all)
{
e[all].y=y;e[all].x=x;e[all].d=d;
e[all].ne=be[x];
be[x]=all++; e[all].y=x;e[all].x=y;e[all].d=d;
e[all].ne=be[y];
be[y]=all++;
} void init()
{
all=all2=;
memset(be,-,sizeof(be));
memset(be2,-,sizeof(be2));
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
fa[i]=i;
} int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
} void tarjan(int u)
{
vis[u]=;
for(int i=be2[u]; i!=-; i=e2[i].ne)
if(vis[e2[i].y])
ancestor[e2[i].d][]=find(e2[i].y); for(int i=be[u]; i!=-; i=e[i].ne)
if(!vis[e[i].y])
{
dis[e[i].y]=dis[u]+e[i].d;
tarjan(e[i].y);
fa[e[i].y]=u;
}
} int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
int x,y,d;
scanf("%d%d",&n,&m);
init();
for(int i=; i<n-; i++)
{
scanf("%d%d%d",&x,&y,&d);
add(x,y,d,e,be,all);
}
for(int i=; i<m; i++)
{
scanf("%d%d",&x,&y);
add(x,y,i,e2,be2,all2);
ancestor[i][]=x;
ancestor[i][]=y;
}
dis[]=;
tarjan();//从根节点开始
for(int i=; i<m; i++)
printf("%d\n",dis[ancestor[i][]]+dis[ancestor[i][]]-*dis[ancestor[i][]]);
}
return ;
}
HDU 4912
Paths on the tree
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 428 Accepted Submission(s): 128
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.
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).
A single integer, the maximum number of paths.
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
2
贪心法,找出给定路径左右节点的最近公共祖先,按其最近公共祖先的深度从大到小插入,每次插入将其子树标记,之后若路径节点若已访问则判不可行,否则ans+1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#define max(x,y) ((x)>(y)?(x):(y))
#define NN 200002 // number of house
using namespace std; int be[NN],all,ans;
bool vis2[NN],vis3[NN]; typedef struct node{
int v;
int d;
struct node *nxt;
}NODE; struct edge{
int u,v,ne;
}e[NN]; NODE *Link1[NN];
NODE edg1[NN * ]; NODE *Link2[NN];
NODE edg2[NN * ]; int idx1, idx2, N, M;
int res[NN][];
int fat[NN];
int vis[NN];
int dis[NN]; void Add(int u, int v, int d, NODE edg[], NODE *Link[], int &idx){
edg[idx].v = v;
edg[idx].d = d;
edg[idx].nxt = Link[u];
Link[u] = edg + idx++; edg[idx].v = u;
edg[idx].d = d;
edg[idx].nxt = Link[v];
Link[v] = edg + idx++;
} int find(int x){
if(x != fat[x]){
return fat[x] = find(fat[x]);
}
return x;
} void Tarjan(int u){
vis[u] = ;
fat[u] = u; for (NODE *p = Link2[u]; p; p = p->nxt){
if(vis[p->v]){
res[p->d][] = find(p->v);
}
} for (NODE *p = Link1[u]; p; p = p->nxt){
if(!vis[p->v]){
dis[p->v] = dis[u] + p->d;
Tarjan(p->v);
fat[p->v] = u;
}
}
} void add(int fa,int x,int y)
{
++all;
e[all].u=x;
e[all].v=y;
e[all].ne=be[fa];
be[fa]=all;
} void color(int u)
{
for (NODE *p = Link1[u]; p; p = p->nxt)
if(vis3[p->v] && !vis2[p->v])
{
vis2[p->v]=;
color(p->v);
}
} void dfs(int u)
{
vis[u]=;
for (NODE *p = Link1[u]; p; p = p->nxt)
if(!vis[p->v]) dfs(p->v); for (int i=be[u]; i!=-; i=e[i].ne)
if(!vis2[e[i].u] && !vis2[e[i].v])
{
vis2[u]=;
ans++;
color(u);
}
vis3[u]=; } int main() {
int T, i, u, v, d;
while(scanf("%d%d", &N, &M)!=EOF)
{
idx1 = ;
memset(Link1, , sizeof(Link1));
for (i = ; i < N; i++){
scanf("%d%d", &u, &v);
d=;
Add(u, v, d, edg1, Link1, idx1);
} idx2 = ;
memset(Link2, , sizeof(Link2));
for (i = ; i <= M; i++){
scanf("%d%d", &u, &v);
Add(u, v, i, edg2, Link2, idx2);
res[i][] = u;
res[i][] = v;
} memset(vis, , sizeof(vis));
dis[] = ;
Tarjan(); all=;
memset(be,-,sizeof(be));
memset(vis,,sizeof(vis));
memset(vis2,,sizeof(vis2));
memset(vis3,,sizeof(vis3));
for(int i=;i<=M; i++)
add(res[i][],res[i][],res[i][]);
for(int i=; i<=N; i++)
fat[i]=i;
ans=;
dfs();
printf("%d\n",ans);
}
return ;
}
HDU 2586 + HDU 4912 最近公共祖先的更多相关文章
- LCA(最近公共祖先)--tarjan离线算法 hdu 2586
HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- hdu 2586(最近公共祖先LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路:在求解最近公共祖先的问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...
- HDU 2586 How far away ?(LCA模板 近期公共祖先啊)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...
- LCA最近公共祖先-- HDU 2586
题目链接 Problem Description There are n houses in the village and some bidirectional roads connecting t ...
- hdu - 2586 How far away ?(最短路共同祖先问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...
- HDU 4547 CD操作 (LCA最近公共祖先Tarjan模版)
CD操作 倍增法 https://i.cnblogs.com/EditPosts.aspx?postid=8605845 Time Limit : 10000/5000ms (Java/Other) ...
- HDU 2586 (LCA模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 / \ 2 3 ...
- HDU - 2586 How far away ?(LCA模板题)
HDU - 2586 How far away ? Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & ...
- HDU 2586 How far away ?【LCA】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...
随机推荐
- ASP.NET Web – 输入的有效性验证
下面这个示例显示了与文本框textFirstname相关的验证控件RequiredFieldValidator.所有的验证控件都有ErrorMessage和ControlToValidate属性.如果 ...
- Sponsored Feature: Common Performance Issues in Game Programming
转自http://www.gamasutra.com/view/feature/132084/sponsored_feature_common_.php?print=1 By Becky Heinem ...
- Careercup - Google面试题 - 4557716425015296
2014-05-03 21:57 题目链接 原题: Many sticks with length, every time combine two, the cost is the sum of tw ...
- cookieContainer应用
PublicSharedFunctionGetCookiesSetByPage(ByVal strUrl AsString,ByVal cookieToProvide AsString)AsIEnum ...
- Oracle 一次执行多条语句
在.Net使用多次方法一次执行多条语句都不成功, 百度了许久才找到正确的解决方案. Oracle执行多条语句的时候 不能有物理换行 写法对比: 如下写法是不成功. begin into t_test ...
- PHP之mysql_real_escape_string()函数讲解
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...
- uva 10617
当s[i] = s[j] dp[i][j] = 1+dp[i+1][j-1]+dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1] = 1+dp[i][j-1]+dp[i+1][j ...
- C Primer Plus之高级数据表示
抽象数据类型(ADT) 类型是由什么组成?一个类型(type)指定两类信息:一个属性集和一个操作集. 所以您想定义一个新的数据类型.首先,您需要提供存储数据的方式,可能是通过设计一个结构.第二 ...
- SQL SERVER(MSSQLSERVER) 服务无法启用 特定服务错误:126
SQL SERVER(MSSQLSERVER) 服务无法启用 特定服务错误:126 对于这样一个错误google了一下 说是 要禁止掉via才行 回到SQL配置管理器中 禁止掉via 果然可以重新 ...
- Java中转UTC时间字符串(含有T Z)为local时间
在Java中我们需要转换相应格式的字符串,很多时候我们想到用SimpleDateFormat类来解析.但是最近我在调用一个第三方的接口时返回的 JSON字符串中有个expires字段的值是2014-0 ...