hdu 4674 Trip Advisor(缩点+倍增lca)】的更多相关文章

花了一天半的时间,才把这道题ac= = 确实是道好题,好久没敲这么长的code了,尤其是最后的判定,各种销魂啊~ 题目中给出的条件最值得关注的就是:每个点最多只能在一个环内->原图是由一个个边连通分量以树形连接组成的->做无向图缩点后,得到的是一个树形结构. 题目要求:u->v,必须经过p,且不能重复经过同一个点,即在树上从u到v做一笔画. 开始先想到汉密尔顿迹,不过那是走全部点的.利用已获得的树形结构,通过lca来判断p,这就是一个合理的作法. 注意:由于是任意建树,p不一定是u,v的…
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…
题目链接: H - Rescue the Princess  ZOJ - 4097 学习链接: zoj4097 Rescue the Princess无向图缩点有重边+lca - lhc..._博客园 题目大意: 首先是T组测试样例,然后是n个点,m条双向边.然后给你u,v,w.问你v和w是否能够到达u,两个人走过的边不能有重复,否则这条边会被压塌. 具体思路:首先对能形成连通块的进行缩点,构成一个个的连通图.然后这样整个图就变成了一个森林,然后再根据染色后的连通块重新建图. 对于每一次的询问,…
传送门 总算是做上一道LCA的应用题了... 题意:有$n$个牧场, $m$根管道分别连接编号为$u,v$的牧场花费$p_{i}$,在第$i$个牧场挖口井需要花费$w_{i}$,有$P$根管道直接连通着$u,v$,即免费连上$u,v$ 对每根免费管道输出让所有牧场都有水的最小花费 先是最小生成树,用0去连每一个点,边权就是每个点的权值,然后正常建,跑一遍最小生成树,把用到的边重新建一次图,然后就对每次询问的$u,v$,减掉他们之间的路径的最长边就是答案了 因为删去这其中一条边,再免费连上$u,v…
Problem Description There is a strange country somewhere which its transportation network was built following some weird rules: Consider the transportation network as a connected undirected graph G with N nodes and M edges(nodes indicate cities,and p…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16425    Accepted Submission(s): 6252 Problem Description There are n houses 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].…
题目传送门 题目描述 给定一个有n个顶点的有向图,每个顶点有且仅有一条出边.每次询问给出两个顶点${a}_{i}$和${b}_{i}$​​,求满足以下条件的${x}_{i}$和${y}_{i}$:    • 从顶点${a}_{i}$沿出边走${x}_{i}$步与从顶点${b}_{i}$​​沿出边走${y}_{i}$步到达的顶点相同.    • $max({{x}_{i}},{{y}_{i}})$最小.    • 满足以上条件的情况下$min({{x}_{i}},{{y}_{i}})$最小.   …
倍增LCA板子,没有压行,可读性应该还可以.转载请随意. #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int maxn = 5e5; int n, m, rt; vector<int> G[maxn]; ; ]; int dep[maxn]; void dfs(int u, int fa…
题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his barn (), conveniently numbered . Each pipe connects a pair of stalls, and all stalls are connected to each-other via paths of pipes. FJ is pumping milk…