日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1021. Deepest Root (25)

發布時間:2024/4/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。