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): 13821    Accepted Submission(s):
5195

Problem Description
  There are n houses in the village and some
bidirectional roads connecting them. Every day peole always like to ask like
this "How far is it if I want to go from house A to house B"? Usually it hard to
answer. But luckily int this village the answer is always unique, since the
roads are built in the way that there is a unique simple path("simple" means you
can't visit a place twice) between every two houses. Yout task is to answer all
these curious people.
 
Input
  First line is a single integer T(T<=10), indicating
the number of test cases.
  For each test case,in the first line there are
two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses
and the number of queries. The following n-1 lines each consisting three numbers
i,j,k, separated bu a single space, meaning that there is a road connecting
house i and house j,with length k(0<k<=40000).The houses are labeled from
1 to n.
    Next m lines each has distinct integers i and j, you areato answer
the distance between house i and house j.
 
Output
  For each test case,output m lines. Each line represents
the answer of the query. Output a bland line after each test case.
 
Sample Input
  2
  3 2
  1 2 10
  3 1 15
  1 2
  2 3
  
  2 2
  1 2 100
  1 2
  2 1
 
Sample Output
  10
25
100
100
 
Source
  题目大意:有T组数据 每组给出n个点由n-1条路连接,给出m次询问,求a和b两个村庄的距离
  我实在不想吐槽,多组数据不用初始化也能A?中间把最近公共祖先输出了也能A!?逗我玩呢。
  带领小学妹谢JustPenz233
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6. vector<int> v[];
  7. vector<int> w[];
  8. int f[][];//f[i][j]表示i点向上2^j层的祖先
  9. int g[][];//g[i][j]表示i点到从i向上2^j层的祖先的距离
  10. int dep[];
  11. int n,m;
  12. void dfs(int pos,int pre,int depth)
  13. {
  14. dep[pos]=depth;
  15. for(int i=;i<v[pos].size();i++)
  16. {
  17. int t=v[pos][i];
  18. if(t==pre) continue;
  19. f[t][]=pos;
  20. g[t][]=w[pos][i];
  21. dfs(t,pos,depth+);
  22. }
  23. }
  24. int query(int a,int b)
  25. {
  26. int sum=;
  27. if(dep[a]<dep[b]) swap(a,b);//深度较深的点
  28. for(int i=;i>=;i--)//找到a在深度dep[b]处的祖先
  29. {
  30. if(dep[f[a][i]]>=dep[b])
  31. {
  32. sum+=g[a][i];//a到该祖先的距离
  33. a=f[a][i];
  34. }
  35. }
  36. if(a==b) return sum;//挪到相同深度后如果在同一点直接return
  37. int x;
  38. for(int i=;i>=;i--)//否则a和b一起往上蹦跶
  39. {
  40. if(f[a][i]!=f[b][i])
  41. {
  42. sum+=g[a][i];
  43. a=f[a][i];
  44. sum+=g[b][i];
  45. b=f[b][i];
  46. }
  47. }
  48. return sum+g[a][]+g[b][];//最后蹦跶到最近公共祖先的下一层,所以要再加上上一层
  49. }
  50. int main()
  51. {
  52. int T;
  53. cin>>T;
  54. while(T--)
  55. {
  56. scanf("%d%d",&n,&m);
  57. memset(dep,-,sizeof dep);//多组数据我们初始化
  58. memset(f,,sizeof f);
  59. memset(g,,sizeof g);
  60. for(int i=;i<n;i++)//md
  61. v[i].clear(),w[i].clear();
  62. for(int i=;i<n;i++)
  63. {
  64. int x,y,c;
  65. cin>>x>>y>>c;
  66. v[x].push_back(y);
  67. w[x].push_back(c);
  68. v[y].push_back(x);
  69. w[y].push_back(c);
  70. }
  71. int xxx=v[].size();
  72. dfs(,,);//dfs处理出每个点的深度,以及各种...
  73.  
  74. for(int i=;<<i<=n;i++)
  75. for(int j=;j<=n;j++)
  76. f[j][i]=f[f[j][i-]][i-],
  77. g[j][i]=g[f[j][i-]][i-]+g[j][i-];
  78. for(int i=;i<=m;i++)
  79. {
  80. int x,y;
  81. cin>>x>>y;
  82. if(x==y) cout<<""<<endl;
  83. else cout<<query(x,y)<<endl;
  84. }
  85. }
  86. return ;
  87. }

HDU2586How far away ?的更多相关文章

  1. hdu-2586-How far away ?(离线LCA)

    题意: 给定一棵树,每条边都有一定的权值,q次询问,每次询问某两点间的距离. 分析: 这样就可以用LCA来解,首先找到u, v 两点的lca,然后计算一下距离值就可以了. 这里的计算方法是,记下根结点 ...

  2. hdu2586How far away ?-(LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:有n个点,有n-1条线连通,求两点间的最短距离,最近公共祖先的入门题.Tarjan离线算法. #in ...

  3. hdu2586How far away ?(LCA LCATarjan离线)

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:有n个点,同n-1条带有权值的双向边相连,有m个询问,每个询问包含两个数x,y,求x与y的最短距离. ...

  4. HDU2586---How far away ?(lca算法)

    Problem Description There are n houses in the village and some bidirectional roads connecting them. ...

  5. 先说IEnumerable,我们每天用的foreach你真的懂它吗?

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  6. 平台之大势何人能挡? 带着你的Net飞奔吧!

    镇楼图: 跨平台系列: Linux基础 1.Linux基础学习 By dnt http://www.cnblogs.com/dunitian/p/4822807.html 环境配置 1.Hyper-v ...

  7. 谈谈一些有趣的CSS题目(十一)-- reset.css 知多少?

    开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...

  8. 百度推出新技术 MIP,网页加载更快,广告呢?

    我们在2016年年初推出了MIP,帮助移动页面加速(原理).内测数据表明,MIP页面在1s内加载完成.现在已经有十多家网站加入MIP项目,有更多的网站正在加入中.在我们收到的反馈中,大部分都提到了广告 ...

  9. 哪种缓存效果高?开源一个简单的缓存组件j2cache

    背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...

随机推荐

  1. anjularjs常用的内置方法

    1.$apply anjularjs通过$apply方法去执行脏检查,及时告诉model绑定变量更新. 示例:定义一个date变量显示在页面上,它的控制器是firstCtrl,这个date变量有一个时 ...

  2. Unity 编译apk启动出异常

    问题:unity 编译出来的apk,在android安装启动,时报以下错误: 07-06 20:52:48.282: E/linker(18229): load_library(linker.cpp: ...

  3. IntelliJ IDEA 12 与 Tomcat7 配置

    IDEA 全称 IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本工具( ...

  4. SqlServer try catch 捕获不到的一些错误及解决方法(转载)

    测试注意 :①假如系统能捕获异常 ,并且我们自己开启了事务.系统会自动 回滚事务的,但是 我们还是要在catch里面加上 rollback tran的习惯,这样也不会提示重复rollback的错误,这 ...

  5. EF 延迟加载和预先加载

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨延迟加载和预先加载 Entity Frame ...

  6. CUtilityCode

    (1) 基于boost的生产者/消费者队列 template<typenameData> classconcurrent_queue { private: std::queue<Da ...

  7. grunt 入门学习

    前端工作流,Grunt上手指南 Posted@2013-04-20 7:15 a.m. CategoriesGrunt ,  javascript 我想先花点时间回忆一下作为一个前端需要做的工作(Lo ...

  8. android Dialog&AlertDialog

    Dialog dialog = new Dialog(context,R.style.AppBaseTheme); wifiView = AppData.inflater.inflate(R.layo ...

  9. Win7开机登陆密码忘记了?不必重做系统(详图)

     1)如果是普通账户密码忘了.方法:重新启动电脑,启动到系统登录界面时,同时按住Ctrl+Alt键,然后连击Del键两次,会出现新的登录界面,用户名处输入“Administrator”密码为空,回车即 ...

  10. Java多线程开发系列之二:如何创建多线程

    前文已介绍过多线程的基本知识了,比如什么是多线程,什么又是进程,为什么要使用多线程等等. 在了解了软件开发中使用多线程的基本常识后,我们今天来聊聊如何简单的使用多线程. 在Java中创建多线程的方式有 ...