1021 Deepest Root(甲級)
1021 Deepest Root (25分)
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 called the deepest root.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤10
4
) 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:
5
1 2
1 3
1 4
2 5
Sample Output 1:
3
4
5
Sample Input 2:
5
1 3
1 4
2 5
3 4
#include<iostream>
#include<vector>
#include<memory.h>
using namespace std;
int n;
const int maxn = 10001;
vector<int>v[maxn];
bool vist[maxn]{ false };
int max_depth = -1;//統計以任意節點作為根的最大深度,也就是該顆樹的最大深度
int max_tmp = -1;//記錄所有節點的最大深度,所有樹中的最大深度
void dfs(int root, int depth)
{
if (max_depth < depth)//求最大深度
{
max_depth = depth;
}
vist[root] = true;
for (int i = 0; i < v[root].size(); i++)
{
if (!vist[v[root][i]])
{
dfs(v[root][i], depth + 1);
}
}
}
int main()
{
cin >> n;
vector<int>count;
for (int i = 0; i < n - 1; i++)
{
int value1, value2;
cin >> value1 >> value2;
v[value1].push_back(value2);
v[value2].push_back(value1);
}
for (int i = 1; i <= n; i++)
{
int cnt = 0;
max_depth = 0;
memset(vist, false, maxn);
if (i == 1)//先判斷是否是聯通的
{
for (int j = 1; j <= n; j++)
{
if (!vist[j])
{
cnt++;
dfs(j, 1);
}
}
if (cnt >= 2)
{
cout << "Error: " << cnt << " components";
return 0;
}
}
else
{
dfs(i, 1);
}
if (max_tmp < max_depth)//更新最大深度,並將根節點入vector
{
count.clear();
max_tmp = max_depth;
count.push_back(i);
}
else if (max_tmp == max_depth)//相等,根節點入vector
{
count.push_back(i);
}
}
for (int i = 0; i < count.size(); i++)
{
cout << count[i] << endl;
}
}
相關文章
- 【PAT乙級】1021 個位數統計
- PTA甲級——Be Unique
- PAT甲級1032 Sharing
- PAT甲級1030 Travel Plan
- 浙大PAT甲級考試
- 取消 root 級管理員的 root 許可權
- PAT甲級1023 Have Fun with Number
- PTA甲級 1076 Forwards on Weibo (30分)Forward
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- 20年春季甲級pat考試
- PAT甲級-1015. Reversible Primes (20)
- PAT甲級1126~1130|C++實現C++
- 1021. 個位數統計 (15)
- PAT甲級-1014. Waiting in Line (30)(模擬)AI
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級考試題庫題目分類
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- 2024 秋季PAT認證甲級(題解A1-A4)
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- LeetCode C++ 1302. Deepest Leaves Sum【Tree/BFS/DFS】中等LeetCodeC++
- 前端面試每日 3+1 —— 第1021天前端面試
- 夕甲甲——孔乙己之C++版C++
- 2021.9.12週六PAT甲級考試覆盤與總結
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- PAT-B 1021 個位數統計【對映】
- 【PTA甲級、C++簡單解答】1001 A+B Format (20分)C++ORM
- 馬甲包
- Starting AHF Services 使用root使用者進行升級操作
- 【南京瑪麗甲狀腺醫院】甲狀腺
- CBRE:2022年成都甲級寫字樓租戶普查
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- 2020年7月第2題 PAT甲級真題 The Judger (25分)
- Maximize the Root