#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. CPU 的工作原理

    内部架构 CPU 的根本任务就是执行指令,对计算机来说最终都是一串由 0 和 1 组成的序列.CPU 从逻辑上可以划分成 3 个模块,分别是控制单元.运算单元和存储单元 .其内部架构如下: [1]控制 ...

  2. mysql双机互相备份

    互备/***************************************master服务器**************************************/vi my.cnf[ ...

  3. [Swift实际操作]九、完整实例-(5)创建BaseViewController作为控制器的基类

    本文将给项目中的所有视图控制器,创建一份基类.该基类用来定义一些共用的属性和方法. 首先在用来放置视图控制器类的文件夹上点击鼠标右键,打开右键 菜单. 选择[New File]创建文件选项. 在弹出的 ...

  4. MyBatis配置文件mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...

  5. 网络控制芯片AX88796B系列使用简介

    目录 1. 特性 2. 结构框图 3. 接收 3.1 缓存空间 3.2 Receiver Buffer Ring 3.3 接收机制 4. 发送 5. 编程过程简要说明 5.1 初始化配置 5.2 接收 ...

  6. Tensorlflow-神经网络解决非线性回归问题

    import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt #使用numpy生成200个随机点,范围从-0.5到0 ...

  7. How to download Heavy Duty Diagnostic Caterpillar SIS 2018 software

    Maybe you find there are supplied Caterpillar SIS 2018 software free download in search engine, that ...

  8. 打ms15-034补丁出现“此更新 不适用于您的计算机”

    1.MS15-034漏洞的补丁是KB3042553; 2.如果在一台Windows Server 2012 R2的服务器上直接安装补丁文件KB3042553,可能会出现“此更新 不适用于您的计算机”的 ...

  9. 邮件email

    参考地址:https://blog.csdn.net/baidu_30000217/article/details/52942258 邮箱配置地址:http://service.exmail.qq.c ...

  10. 使用JavaFX打开fxml,找不到打开的图形界面

    解决方案: 将 HKEY_CURRENT_USER\Software\JavaSoft\Prefs\com\oracle\javafx\scenebuilder\app\preferences\/S/ ...