树上倍增 hdu 2586】的更多相关文章

参考博客: 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #include<string.h> using namespace std; ; int depth[max_];//节点深度 ];//第 i 点到2^j父亲的距离 ];//第 i 点的2^j父亲 bool vis[max_];//标记数组 int tot,N; struct T…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4822 Problem Description Three countries, Red, Yellow, and Blue are in war. The map of battlefield is a tree, which means that there are N nodes and (N – 1) edges that connect all the nodes. Each country…
hdu 2586 How far away ?倍增LCA 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2586 思路: 针对询问次数多的时候,采取倍增求取LCA,同时跟新距离数组 因为 \(2^{16} > 40000\) 所以所以表示祖先的数组dp[][]第二维取到16即可 就这道题来说,与比较tarjan比较,稍快一点 代码: #include <iostream> #include <algorithm> #includ…
How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11699    Accepted Submission(s): 4300 Problem Description There are n houses in the village and some bidirectional roads connectin…
先瞎扯几句 树上倍增的经典应用是求两个节点的LCA 当然它的作用不仅限于求LCA,还可以维护节点的很多信息 求LCA的方法除了倍增之外,还有树链剖分.离线tarjan ,这两种日后再讲(众人:其实是你不会吧:unamused:...) 思想 树上倍增嘛,顾名思义就是倍增 相信倍增大家都不默认,著名的rmq问题的$O(n*logn)$的解法就是利用倍增实现的 在树上倍增中,我们用 $f[j][i]$表示第$j$号节点,跳了$2^j$步所能到达的节点 $deep[i]$表示$i$号节点的深度 然后用…
倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ]; void dfs(int x,int fa) { int i,k; vis[x]=; anc[x][]=fa; deep[x]=deep[fa]+; ;i<=log2n;i++) anc[x][i]=anc[anc[x][i-]][i-]; ;k=e[k].next) if(!vis[e[k].…
HDU 2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11320    Accepted Submission(s): 4119 Problem Description   There are n houses in the village and some bidirectional roads…
题目描述 Description 小机房有棵焕狗种的树,树上有N个节点,节点标号为0到N-1,有两只虫子名叫飘狗和大吉狗,分居在两个不同的节点上.有一天,他们想爬到一个节点上去搞基,但是作为两只虫子,他们不想花费太多精力.已知从某个节点爬到其父亲节点要花费 c 的能量(从父亲节点爬到此节点也相同),他们想找出一条花费精力最短的路,以使得搞基的时候精力旺盛,他们找到你要你设计一个程序来找到这条路,要求你告诉他们最少需要花费多少精力 输入描述 Input Description 第一行一个n,接下来…
死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每…
HDU - 2586 How far away ? Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like th…