1021. Deepest Root (25)
来源:互联网 发布:大华图像算法工程师 编辑:程序博客网 时间:2024/05/21 12:47
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on the selected root. Now you are supposed to find the root that results in a highest tree. Such a root is calledthe deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the number of nodes, and hence the nodes are numbered from 1 to N. Then N-1 lines follow, each describes an edge by given the two adjacent nodes' numbers.
Output Specification:
For each test case, print each of the deepest roots in a line. If such a root is not unique, print them in increasing order of their numbers. In case that the given graph is not a tree, print "Error: K components" where K is the number of connected components in the graph.
Sample Input 1:51 21 31 42 5Sample Output 1:
345Sample Input 2:
51 31 42 53 4Sample Output 2:
Error: 2 components
1. 由于N个节点,N-1条边,则保证如果只有一个连通集,则一定是树,而不可能有环,而连通集个数可以通过一遍DFS得到;
2. 任取一点DFS,将得到的最远点集合记为s1;
3. 从s1中任取一点,再来一遍DFS,将得到的最远点集合记为s2;
4. 合并s1,s2得到s,即为所有根;
#include <iostream>#include <cstdio>#include <vector>#include <algorithm>#include <limits>#include <set>#include <functional>using namespace std;vector<vector<bool>> graph;int n;int kcomponents(){ vector<bool> used(n+1, false); function<void(int)> dfs = [&](int node){ used[node] = true; for(int i = 1; i <= n; ++i){ if(!used[i] && graph[node][i]) dfs(i); } }; int k = 0; for(int i = 1; i <= n; ++i){ if(!used[i]) dfs(i), ++k; } return k;}void DFS(int node, int level, int& maxLevel, vector<int>& nodes, vector<bool>& used){ if(level > maxLevel){ maxLevel = level; nodes = vector<int>(1, node); }else if(level == maxLevel){ nodes.push_back(node); } for(int i = 1; i <= n; ++i){ if(!used[i] && graph[node][i]){ used[i] = true; DFS(i, level + 1, maxLevel, nodes, used); used[i] = false; } }}int main(){ scanf("%d", &n); graph.resize(n+1, vector<bool>(n+1, false)); for(int i = 0; i < n-1; ++i){ int n1, n2; scanf("%d%d", &n1, &n2); graph[n1][n2] = graph[n2][n1] = true; } int k = kcomponents(); if(k > 1) printf("Error: %d components\n", k); else{ int maxLevel = numeric_limits<int>::min(); vector<int> nodes; vector<bool> used(n+1, false); used[1] = true; DFS(1, 0, maxLevel, nodes, used); used[1] = false; set<int> sets(begin(nodes), end(nodes)); int node = nodes.back(); nodes.clear(); used[node] = true; maxLevel = numeric_limits<int>::min(); DFS(node, 0, maxLevel, nodes, used); for(auto& node : nodes){ sets.insert(node); } for(auto& node : sets){ printf("%d\n", node); } } return 0;}
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 1021. Deepest Root (25)
- 最短路算法详解(Dijkstra/SPFA/Floyd)
- 配置Tomcat+MySQL单点登录服务器
- PHP之static
- 解决Ubuntu重启后resolv.conf清空nameserver的问题
- C++操作二进制文件
- 1021. Deepest Root (25)
- [BZOJ1975][Sdoi2010]魔法猪学院
- java - 常见面试题
- 腾讯云服务器数据跨地域转移复制方法腾讯云不同区域数据复制
- 镇楼
- 卸载lantern后ie无法上网问题。卸载wifi共享精灵后无法上网
- 统计学习方法概论
- POJ 3693(后缀数组)
- ZOJ 3480 Duck Typing