题目地址:https://www.nowcoder.com/acm/contest/136/C

dfs找出最长路和次长路,将两个结果相加再加上起点即可;

 #include<iostream>
using namespace std; const int N = 1e6+;
struct EDGE {
int to, pre;
}edge[*N];
int n, tot=, head[N], first[N], second[N], ans=; void read(int &x) {
int f = ; x = ;
char ch = getchar(); while (ch < '' || ch > '') {if (ch == '-') f = -; ch = getchar();}
while (ch >= '' && ch <= '') {x = x * + ch - ''; ch = getchar();}
x *= f;
} void add_edge(int a, int b)
{
edge[tot].to = b;
edge[tot].pre = head[a];
head[a] = tot++;
} void dfs(int k, int pre)
{
for(int i=head[k]; i; i=edge[i].pre) {
int to = edge[i].to;
if(to == pre) continue; dfs(to, k);
if(first[k] < first[to]+) {
second[k] = first[k];
first[k] = first[to]+;
}
else
second[k] = max(second[k], first[to]+);
}
ans = max(ans, first[k]+second[k]);
} int main()
{
read(n);
for(int a,b,i=; i<n; i++) {
read(a); read(b);
add_edge(a,b);
add_edge(b,a);
} dfs(, );
cout<<ans+<<endl; return ;
}

最新文章

  1. [BI项目记]-新任务处理
  2. Java编程中“为了性能”尽量要做到的一些地方
  3. MEF入门之不求甚解,但力求简单能讲明白(四)
  4. setNeedsDisplay 和 setNeedsLayout-b
  5. C++ : 类型的别名和对象的别名
  6. 写给C语言新手的话
  7. SQL Server 2005中设置Reporting Services发布web报表的匿名访问
  8. UNIX网络编程——客户/服务器程序设计示范(总结)
  9. Docker 下载镜像
  10. swift两种获取相册资源PHAsset的路径的方法(绝对路径)
  11. JWT 理解
  12. .Net桌面程序自动更新NAppUpdate
  13. HTML标签参考(一)
  14. c#将Excel数据导入到数据库的实现代码
  15. ARM 编译工具keil 和 IAR 命令行编译和下载
  16. Tarjan算法初探 (1):Tarjan如何求有向图的强连通分量
  17. supervisor 添加新配置不生效的问题
  18. MIPS 汇编指令学习
  19. 互评Alpha版本
  20. Android中的WebView进行直接加载网页(要注意解决权限问题)

热门文章

  1. bzoj4390
  2. HDU 5512 Pagodas (数论)
  3. quickpow || 快速幂
  4. logstsh | logstash-input-jdbc 启动错误收集
  5. [POJ2750]Potted Flower
  6. 编译原理 LR分析(主要是LR(0)分析)
  7. 应用交付、负载均衡(Load balancing)、高可用、F5
  8. Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏
  9. Android插件开发
  10. spark源码编译,运行example遇到:NoClassDefFoundError: org/spark_project/guava/cache/CacheLoader