先给个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

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
 

贪心法,找出给定路径左右节点的最近公共祖先,按其最近公共祖先的深度从大到小插入,每次插入将其子树标记,之后若路径节点若已访问则判不可行,否则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 最近公共祖先的更多相关文章

  1. LCA(最近公共祖先)--tarjan离线算法 hdu 2586

    HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  2. hdu 2586(最近公共祖先LCA)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路:在求解最近公共祖先的问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  3. HDU 2586 How far away ?(LCA模板 近期公共祖先啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the vi ...

  4. LCA最近公共祖先-- HDU 2586

    题目链接 Problem Description There are n houses in the village and some bidirectional roads connecting t ...

  5. hdu - 2586 How far away ?(最短路共同祖先问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 最近公共祖先问题~~LAC离散算法 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起 ...

  6. HDU 4547 CD操作 (LCA最近公共祖先Tarjan模版)

    CD操作 倍增法  https://i.cnblogs.com/EditPosts.aspx?postid=8605845 Time Limit : 10000/5000ms (Java/Other) ...

  7. HDU 2586 (LCA模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 ...

  8. HDU - 2586 How far away ?(LCA模板题)

    HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

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

随机推荐

  1. Halcon学习笔记之缺陷检测(二)

    例程:detect_indent_fft.hdev 说明:这个程序展示了如何利用快速傅里叶变换(FFT)对塑料制品的表面进行目标(缺陷)的检测,大致分为三步: 首先,我们用高斯滤波器构造一个合适的滤波 ...

  2. ProgressDialog弹出时的底色变暗(转)

    背景:在做一个进度条时,不想让它的背景变暗,以免影响其他区域的正常显示. 在网上搜索时,看到的方法多数是: 方法一 :在代码中 可以这么设置 Window mWindow = getWindow(); ...

  3. 将Asp.Net MVC应用程序的控制器定义在单独的程序集(类库)中

    一直以来都想把控制器的代码部署到单独的程序集里.昨天研究Asp.Net MVC的源代码,偶然发现有一个奇特的类“ControllerBuilder”,MSDN上的介绍相当简略,就一句话“表示一个类,该 ...

  4. spring mvc 学习笔记【1】---前言

    扎扎实实地掌握好技术 ----------------------------------------------------------------------------------------- ...

  5. 51nod 1640 天气晴朗的魔法 最小生成树

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 题解: 先求最小生成树,记录最大边. 然后求最大生成树 ...

  6. Linux/Ubuntu常用快捷键

    问题描述:         Linux/Ubuntu常用快捷键   问题解决: +++++++++++++++++++ 全局系统 +++++++++++++++++++++ Alt + F1:相当于w ...

  7. SQL Server 之 校对

    _CI(CS) 是否区分大小写,CI不区分,CS区分 _AI(AS) 是否区分重音,AI不区分,AS区分 _KI(KS) 是否区分假名类型,KI不区分,KS区分 _WI(WS) 是否区分宽度 WI不区 ...

  8. FolderBrowserDialog 成员

    http://msdn.microsoft.com/zh-cn/library/system.windows.forms.folderbrowserdialog_members(v=vs.80).as ...

  9. 高性能网络编程2----TCP消息的发送

    转 陶辉 taohui.org.cn 在上一篇中,我们已经建立好的TCP连接,对应着操作系统分配的1个套接字.操作TCP协议发送数据时,面对的是数据流.通常调用诸如send或者write方法来发送数据 ...

  10. 去除List集合中的重复对象,Map遍历代码

    /*** * 去除List<PartsInfoDTO>列表中的重复对象 ~!! * @param list * @return */ public static List<Parts ...