1. //Accepted 740 KB 15 ms
  2. //树的直径
  3. //距离一个顶点最远的点一定是树的直径的一个端点
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <queue>
  7. #include <iostream>
  8. using namespace std;
  9. ;
  10. int dis[imax_n];
  11. int d1[imax_n];
  12. int d2[imax_n];
  13. int head[imax_n];
  14. *imax_n];
  15. int e;
  16. struct node
  17. {
  18. int u,v,c;
  19. node()
  20. {
  21.  
  22. }
  23. node(int u,int v,int c)
  24. {
  25. this->u=u;
  26. this->v=v;
  27. this->c=c;
  28. }
  29. }p[*imax_n];
  30. int n;
  31. void addEdge(int u,int v,int c)
  32. {
  33. p[e]=node(u,v,c);
  34. next[e]=head[u];
  35. head[u]=e++;
  36. }
  37. void init()
  38. {
  39. memset(head,-,sizeof(head));
  40. memset(next,-,sizeof(next));
  41. e=;
  42. }
  43. void read()
  44. {
  45. init();
  46. int x,y;
  47. ;i<=n;i++)
  48. {
  49. scanf("%d%d",&x,&y);
  50. addEdge(i,x,y);
  51. addEdge(x,i,y);
  52. }
  53. }
  54. int max(int a,int b)
  55. {
  56. return a>b?a:b;
  57. }
  58. queue<int >q;
  59. bool vis[imax_n];
  60. int bfs(int s)
  61. {
  62. //printf("111111\n");
  63. memset(dis,,sizeof(dis));
  64. ;
  65. int temp_node;
  66. while (!q.empty()) q.pop();
  67. memset(vis,,])*(n+));
  68. q.push(s);
  69. vis[s]=;
  70. while (!q.empty())
  71. {
  72. int x=q.front();
  73. if (dis[x]>ans)
  74. {
  75. ans=dis[x];
  76. temp_node=x;
  77. }
  78. q.pop();
  79. ;i=next[i])
  80. {
  81. int y=p[i].v;
  82. if (!vis[y])
  83. {
  84. vis[y]=;
  85. q.push(y);
  86. if (dis[x]+p[i].c>dis[y])
  87. dis[y]=dis[x]+p[i].c;
  88. }
  89. }
  90. }
  91. return temp_node;
  92. }
  93. int main()
  94. {
  95. while (scanf("%d",&n)!=EOF)
  96. {
  97. read();
  98. );
  99. x=bfs(x);
  100. memcpy(d1,dis,sizeof(dis));
  101. bfs(x);
  102. memcpy(d2,dis,sizeof(dis));
  103. ;i<=n;i++)
  104. {
  105. printf("%d\n",max(d1[i],d2[i]));
  106. }
  107. }
  108. ;
  109. }

hdu2196 树的直径 + bfs的更多相关文章

  1. poj2631 树的直径 + bfs

    //Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...

  2. 树上选两点(使最短)树的直径+bfs

    题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...

  3. ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS

    题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...

  4. luogu P3761 [TJOI2017]城市 树的直径 bfs

    LINK:城市 谢邀,学弟说的一道毒瘤题. 没有真正的省选题目毒瘤 或者说 写O(n)的做法确实毒瘤. 这里给一个花20min就写完的非常好写的暴力. 容易想到枚举哪条边删掉 删掉之后考虑在哪两个点上 ...

  5. POJ1985 树的直径(BFS

    Cow Marathon   Description After hearing about the epidemic of obesity in the USA, Farmer John wants ...

  6. 树的直径(BFS)

    ][];];];];,,;vis[i]=; ; j <= n ; j++){ ){;//标记 res[j]=res[root]+; ; i <= n- ; i++){; data[b][a ...

  7. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  8. 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。

    问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...

  9. HDU4612+Tarjan缩点+BFS求树的直径

    tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...

随机推荐

  1. Webdriver - Selenium Grid Configuration

    Grid parameter: role = <hub|node> (default is no grid, just run an RC/webdriver server). When ...

  2. jmeter 建立一个网络服务的测试计划

    如何创建一个 测试计划 测试一个网络服务. 你会 创建5个用户发送请求到一页. 同时,你会告诉用户运行测试两次. 的总数 请求用户请求(5)x(1)x(重复2次)= 10 HTTP请求. 来 建立测试 ...

  3. jmeter 构建一个Web测试计划

    添加用户 第一步你想做的每一个JMeter测试计划是添加一个 线程组 元素. 线程组告诉 JMeter的用户数量你想模拟,用户应该发送的次数 请求,请求他们应该送多少. 继续添加ThreadGroup ...

  4. 加载图片、倒计时--Columbia项目总结

    CSS3 居然会在隐藏显示的时候触发,真心太神奇了(在ff下是没有用的,在chrome下才阔以) 貌似我还是写了大循环,怎么样才能优化这个循环呢 加载图片:已经用到过好多次了↓ function lo ...

  5. Ubuntu下配置和编译cpp-ethereum客户端

    Ethereum,中文翻译是“以太坊”,是一个公有区块链的开源项目.因为以太坊是基于P2P网络所以没有中心节点,所以用户仅安装Ethereum客户端即可连入Ethereum公共网络或者在自己的test ...

  6. BZOJ1737 [Usaco2005 jan]Naptime 午睡时间

    断环然后裸DP就好了... $f[i][j][k]$表示1号时间段没有被算入答案,到了第$i$个时间段,一共选了$j$个时间段,$k = 0 /1$表示第i个时间段有没有被算进答案的最优值 $g[i] ...

  7. 源码编译Chrome

    官网描述 http://www.chromium.org/developers/how-tos/build-instructions-windows 为啥还要写这篇博客 太久没在这里写博客 Chrom ...

  8. input 中的enabled与disabled属性

    <style type="text/css"> *{ padding:; margin:; list-style-type: none; box-sizing:bord ...

  9. linux 查看僵尸进程

    top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...

  10. PDF 补丁丁 0.4.1.688 测试版发布(请务必用其替换 682 测试版)

    修复了测试版682 损坏书签.读取字符宽度表出错的问题.请下载了旧测试版的网友马上换用新的测试版.