1021. Deepest Root (25)
題目鏈接:http://www.patest.cn/contests/pat-a-practise/1021
題目:
1021. Deepest Root (25)
時間限制 1500 ms內存限制 65536 kB
代碼長度限制 16000 B
判題程序 Standard 作者 CHEN, Yue
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 (<=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: 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 Sample Output 2: Error: 2 components分析:
用并查集來考察能否構成一棵樹,
找出最長樹的深度基于這種一個事實:
從隨意一個節點S0出發,找到最大的深度H0和其相應的葉子節點D0。然后再從D0出發(當作S1)找到最深的長度H1,假設和H0相等則其就是最大的H,否則繼續找,直到兩次找到的H相等。
當中,找最大深度函數find_height()。我是用類似層序遍歷去做的,先把根節點和-1(一個標志位)放入隊列中。然后每次取對頭,假設是元素的話。則把其子節點都放入隊列。假設是-1的話。則把深度++,而且再把-1放入,相當于-1變成了每層的結尾的標志。
當然,求樹最大深度能夠用遞歸非常easy做出來,這里僅僅是用別的方法做個拓展(事實上這代碼曾經寫的,當時瞎想就想到這個)。
AC代碼:
#include<stdio.h> #include<queue> #include<vector> #include<algorithm> using namespace std; vector<int>V[10001]; queue<int>Q; int Tree[10001]; int ans[10001]; int findRoot(int x){if (Tree[x] == -1)return x;else {int tmp = findRoot(Tree[x]);Tree[x] = tmp;return tmp;} } bool mark[10001]; int tail; int n; int find_height(int x){//找到最大深度的函數mark[x] = true;int height_tmp = 0;Q.push(x); Q.push(-1);while (!Q.empty()){if (Q.front() == -1){height_tmp++;Q.pop();if (Q.empty())break;else Q.push(-1);}int front = Q.front();tail = front;for (int i = 0; i < V[front].size(); i++){if (!mark[V[front][i]])Q.push(V[front][i]);mark[V[front][i]] = true;}Q.pop();}for (int i = 1; i <= n; i++){mark[i] = false;}return height_tmp; } int main(void){//freopen("F://Temp/input.txt", "r", stdin);while (scanf("%d", &n) != EOF){for (int i = 1; i <= n; i++){ //initTree[i] = -1;ans[i] = 0;mark[i] = false;}if (!Q.empty())Q.pop();int a, b;for (int i = 0; i < n - 1; i++){//并查集部分scanf("%d%d", &a, &b);V[a].push_back(b);V[b].push_back(a);a = findRoot(a);b = findRoot(b);if (a != b) Tree[a] = b;}int com = 0;for (int i = 1; i <= n; i++){if (Tree[i] == -1)com++;}if (com > 1){//假設并查集找出超過兩部分,則輸出error...printf("Error: %d components", com);continue;}else{int h_max;int head;for (int i = 1; i <= n; i++){if (V[i].size() == 1){head = i;break;}}h_max = find_height(head);while (h_max != find_height(tail)){//假設找到的高度不同樣。則繼續找head = tail;h_max = find_height(head);}int j = 0;for (int i = 1; i <= n; i++){if (find_height(i) == h_max){ans[j++] = i;}}sort(ans, ans + j);for (int i = 0; i < j; i++){printf("%d\n", ans[i]);}}}return 0; }截圖:
——Apie陳小旭
轉載于:https://www.cnblogs.com/yutingliuyl/p/6721486.html
總結
以上是生活随笔為你收集整理的1021. Deepest Root (25)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第15组构建之法团队心得(2)
- 下一篇: Excel工作表密码保护的破解