#include <cstdio>
#include <cstdlib>
#include <vector> using namespace std; class Node {
public:
vector<int> adj;
bool visited;
Node() : visited(false) {}
}; void reset_nodes(vector<Node>& nodes) {
int len = nodes.size();
for (int i=; i<len; i++) {
nodes[i].visited = false;
}
} void dfs(int idx, vector<Node>& nodes, int level, int& deepest) {
if (nodes[idx].visited) return;
Node& node = nodes[idx];
node.visited = true;
if (level > deepest) deepest = level; int len = node.adj.size();
for (int i=; i<len; i++) {
dfs(node.adj[i], nodes, level + , deepest);
}
} int find_deepest_from(int idx, vector<Node>& nodes) {
int deepest = ;
// reset visited flag of all the nodes
reset_nodes(nodes); // find the max level from this node(as root)
dfs(idx, nodes, , deepest); int parts = ;
// check if other parts exist
for (int i = nodes.size() - ; i>=; i--) {
if (!nodes[i].visited) {
int dummy = ;
dfs(i, nodes, , dummy);
parts++;
}
} return parts > ? -parts : deepest;
} int main() {
int N = ;
scanf("%d", &N); vector<Node> nodes(N + ); for (int i=; i<N; i++) {
int a, b;
scanf("%d%d", &a, &b);
nodes[a].adj.push_back(b);
nodes[b].adj.push_back(a);
} int res = ;
vector<int> deepnodes;
int deepest = ;
for (int i=; i<=N; i++) {
res = find_deepest_from(i, nodes);
// not connected graph, stop search
if (res < ) {
break;
}
if (res > deepest) {
deepest = res;
deepnodes.clear();
deepnodes.push_back(i);
} else if (res == deepest) {
deepnodes.push_back(i);
}
}
if (res < ) {
printf("Error: %d components\n", -res);
} else {
int len = deepnodes.size();
for (int i=; i<len; i++) {
printf("%d\n", deepnodes[i]);
}
}
return ;
}

最深root的最远leaf也应该是最深root,利用这个做下优化的话速度应该会快不少。下午改写了一发,时间就有原来的1200ms降到了15ms,可见利用问题中的规律还是很有必要的:

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <set> using namespace std; class Node {
public:
vector<int> adj;
bool visited;
Node() : visited(false) {}
}; void reset_nodes(vector<Node>& nodes) {
int len = nodes.size();
for (int i=; i<len; i++) {
nodes[i].visited = false;
}
} void dfs(int idx, vector<Node>& nodes, int level, int& deepest, set<int>& leaf) {
if (nodes[idx].visited) return;
Node& node = nodes[idx];
node.visited = true;
if (level > deepest) {
deepest = level;
leaf.clear();
leaf.insert(idx);
} else if (level == deepest) {
leaf.insert(idx);
} int len = node.adj.size();
for (int i=; i<len; i++) {
dfs(node.adj[i], nodes, level + , deepest, leaf);
}
} int find_deepest_from(int idx, vector<Node>& nodes, set<int>& leaf) {
int deepest = ;
// reset visited flag of all the nodes
reset_nodes(nodes); // find the max level from this node(as root)
dfs(idx, nodes, , deepest, leaf); int parts = ;
// check if other parts exist
set<int> dummy_leaf;
int dummy_deepest = ;
for (int i = nodes.size() - ; i>=; i--) {
if (!nodes[i].visited) {
dummy_leaf.clear();
dummy_deepest = ;
dfs(i, nodes, , dummy_deepest, dummy_leaf);
parts++;
}
}
if (parts > ) return -parts; reset_nodes(nodes);
vector<int> root(leaf.begin(), leaf.end());
int len = root.size(); for (int i=; i<len; i++) {
dfs(root[i], nodes, , deepest, leaf);
leaf.insert(root[i]);
} return parts > ? -parts : deepest;
} int main() {
int N = ;
scanf("%d", &N); vector<Node> nodes(N + ); for (int i=; i<N; i++) {
int a, b;
scanf("%d%d", &a, &b);
nodes[a].adj.push_back(b);
nodes[b].adj.push_back(a);
} int res = ;
set<int> deepnodes;
int deepest = ;
res = find_deepest_from(, nodes, deepnodes);
// not connected graph
if (res < ) {
printf("Error: %d components\n", -res);
} else {
auto iter = deepnodes.begin();
while (iter != deepnodes.end()) {
printf("%d\n", *iter++);
}
}
return ;
}

PAT 1021 Deepest Root的更多相关文章

  1. PAT 1021 Deepest Root[并查集、dfs][难]

    1021 Deepest Root (25)(25 分) A graph which is connected and acyclic can be considered a tree. The he ...

  2. [PAT] 1021 Deepest Root (25)(25 分)

    1021 Deepest Root (25)(25 分)A graph which is connected and acyclic can be considered a tree. The hei ...

  3. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

  4. PAT 甲级 1021 Deepest Root (并查集,树的遍历)

    1021. Deepest Root (25) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A graph ...

  5. PAT 甲级 1021 Deepest Root (25 分)(bfs求树高,又可能存在part数part>2的情况)

    1021 Deepest Root (25 分)   A graph which is connected and acyclic can be considered a tree. The heig ...

  6. PAT 甲级 1021 Deepest Root

    https://pintia.cn/problem-sets/994805342720868352/problems/994805482919673856 A graph which is conne ...

  7. 1021. Deepest Root (25)——DFS+并查集

    http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...

  8. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  9. 1021. Deepest Root (25) -并查集判树 -BFS求深度

    题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...

随机推荐

  1. 洛谷P4012 深海机器人问题(费用流)

    传送门 图给的好坑……还得倒过来…… 用大佬的图做个示范 我们考虑左图吧 把每一个点向下连边,容量$1$,费用为给出的价值(表示一个机器人可以过去取得标本) 再连一条边,容量$inf$,费用$0$(表 ...

  2. javascript中构造器(函数)的__proto__与prototype初探

    背景:最近没什么需求,快要闲出屁了,所以重温了一下js的原型,结果大有收获,且偶然看到Snandy大神的<JavaScript中__proto__与prototype的关系> 这篇文章,感 ...

  3. 15、OpenCV Python 轮廓发现

    __author__ = "WSX" import cv2 as cv import numpy as np # 基于拓扑结构来发现和绘制(边缘提取) # cv.findConto ...

  4. 创建Oracle synonym 详解

    --创建使用同义词 --同义词就是给表.视图等对象取得别名,用于简化对其的访问 --分为2种: --私有同义词:用户自己创建自己使用的 --公共同义词:dba创建,给其它用户使用的 --为dept_s ...

  5. SDUT OJ 数据结构实验之排序四:寻找大富翁

    数据结构实验之排序四:寻找大富翁 Time Limit: 200 ms Memory Limit: 512 KiB Submit Statistic Discuss Problem Descripti ...

  6. 主机和虚拟机互Ping的问题

    主机能ping通虚拟机,虚拟机能ping不通主机. 发现原来是被防火墙阻止了.打开主机防火墙禁止Ping的方式. 在ping不通的电脑上对防火墙进行如下设置:依次单击“防火墙”—“高级设置”—“入站规 ...

  7. 多线程 NSOpeartion 的使用

    NSOperation简介 相对于 GCD ,具有面向对象的特征,比 GCD 更简单易用,代码可读性强 NSOperatioin 单独使用时, 不具有开辟新线程的能力, 只是同步执行操作, 需要配合 ...

  8. C#实现,一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第35位数是多少, 用递归算法实现

    方法函数可以通过调用自身来进行递归.计算理论可以证明递归的作用可以完全取代循环. static void Main(string[] args) { Console.WriteLine(Ra()); ...

  9. HDU_1028 Ignatius and the Princess III 【母函数的应用之整数拆分】

    题目: "Well, it seems the first problem is too easy. I will let you know how foolish you are late ...

  10. 动态规划 70.climbing Stairs

    1. 记忆化搜索 - 自上向下的解决问题:使用vector来保存每次计算的结果,如果下次再碰到同样的需要计算的式子就不需要重复计算了. 2. 动态规划 - 自下向上的解决问题 解法一:自顶向下 解法二 ...