1021. Deepest Root (25)
来源:互联网 发布:nginx根据域名跳转 编辑:程序博客网 时间:2024/04/30 10:36
#include <cstdio>#include <algorithm>#include <vector>#include <cstring>using namespace std;const int maxn = 10010;int deepest = 0;vector<int> city[maxn];int deep_r[maxn], p = 0;bool visit[maxn] = {false};bool visit1[maxn] = {false};void DFS(int root) {visit[root] = true;for (int i = 0; i < city[root].size(); i++) {if (visit[city[root][i]]) continue;DFS(city[root][i]); }}void DFS(int root, int level) {visit[root] = true;bool flag = false;for (int i = 0; i < city[root].size(); i++) {if (visit[city[root][i]]) continue;DFS(city[root][i], level + 1);flag = true;}if (flag == false) {if (level > deepest) {deepest = level;p = 0;memset(visit1, false, sizeof(visit1));}if (level == deepest && visit1[root] == false) {deep_r[p++] = root;visit1[root] = true;}}}int main() {int n;scanf("%d", &n);int c1, c2;for (int i = 1; i < n; i++) {scanf("%d%d", &c1, &c2);city[c1].push_back(c2);city[c2].push_back(c1);}int block = 0;for (int i = 1; i <= n; i++) {if (visit[i] == false) {block++;DFS(i);}}if (block > 1) {printf("Error: %d components\n", block);} else {for (int i = 1; i <= n; i++) {memset(visit, false, sizeof(visit));DFS(i, 0);}sort(deep_r, deep_r + p);for (int i = 0; i != p; i++) {printf("%d\n", deep_r[i]);}}return 0;}
0 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)
- Linux驱动技术(七) _内核定时器与延迟工作
- What is the difference between pages and blocks?
- Java移位运算符及小技巧
- Zepto Click点透情况解决办法
- bugly使用心得
- 1021. Deepest Root (25)
- Hibernate 主键生成
- css设置透明度
- Java初学之抛异常
- ecshop 模板设置详解
- HDU2057_A + B Again_十六进制输入输出
- Hbase学习6_<hostname>:9000 failed on connection exception: java.net.ConnectException: 拒绝连接
- android修改react native依赖包的版本
- StringTokenizer