[HihoCoder]#1050 : 树中的最长路
2016-06-24 00:09
302 查看
华电北风吹
天津大学认知计算与应用重点实验室
2016-06-24
题目链接:
http://hihocoder.com/problemset/problem/1050
题目分析:
知道树中最长路的性质即可。
解题报告:
第一次做的时候不知道规律,知道后就简单了。
天津大学认知计算与应用重点实验室
2016-06-24
题目链接:
http://hihocoder.com/problemset/problem/1050
题目分析:
知道树中最长路的性质即可。
// problem1050.cpp : 定义控制台应用程序的入口点。 // #1050 : 树中的最长路 // 张正义 2016-05-15 #include "stdafx.h" #include <iostream> #include <vector> #include <queue> using namespace std; int BroadVisit(vector<vector<int>> &edgeList, int start, int &end) { int depth = -1; queue<int> q; vector<bool> visited(edgeList.size()); q.push(-1); q.push(start); while (q.empty()==false) { if (q.front() == -1) { q.pop(); q.push(-1); if (q.front() == -1) { break; } depth++; } int nodeNum = q.front(); q.pop(); visited[nodeNum] = true; for (int i = 0; i < edgeList[nodeNum].size(); i++) { if (visited[edgeList[nodeNum][i]] == false) { end = edgeList[nodeNum][i]; q.push(end); } } } return depth; } int main() { int n; cin >> n; vector<vector<int>> edgeList(n); for (int i = 0; i < n - 1; i++) { int p1, p2; cin >> p1 >> p2; p1--; p2--; edgeList[p1].push_back(p2); edgeList[p2].push_back(p1); } int end; BroadVisit(edgeList, 0, end); cout << BroadVisit(edgeList, end, end) << endl; return 0; }
解题报告:
第一次做的时候不知道规律,知道后就简单了。
相关文章推荐
- 用代码创建UIPageControl时,无法显示的问题
- javascript类型系统——正则表达式RegExp类型
- 应用层-个性化推荐系统基本概念及5种常规算法初探
- linux下svn常用指令
- 关于初入信息安全领域的几个概念
- 1046. 划拳(15) PAT 乙级
- $.ajax() 方法的理解
- Html Vedio标签全屏
- [HihoCoder]#1043 : 完全背包
- 如何在 LibreOffice 中创建模板以实现省时高效
- 老司机谈APK瘦身套路-图片资源篇
- 老司机谈APK瘦身套路-图片资源篇
- 移动端点击事件全攻略,这里的坑你知多少?
- app控件获取之uiautomatorviewer
- 详解java WebSocket的实现以及Spring WebSocket
- java WebSocket的实现以及Spring WebSocket示例代码
- 女程序员在互联网界到底有没有被歧视?
- 小谈Java中的静态块与非静态块
- JavaWeb实现图形报表折线图的方法
- 只需三步,教你做好需求分析