1. 程式人生 > >程式設計基礎28 關於圖的遍歷(二)

程式設計基礎28 關於圖的遍歷(二)

1021 Deepest Root (25 分)

A graph which is connected and acyclic can be considered a tree. The hight 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

Sample Output 2:

Error: 2 components

這道題的解題經歷一波三折。

一,過程一:

    先用並查集找出有幾個連通塊,若有一個連通塊進入以下程式碼,問題出在blot_num==1之後的for迴圈中,本來的意思是先把全域性變數max_depth在進入子函式前賦給一個depth,然後比較出了這個函式之後max_depth是否發生變化,但忽略了一點,若假設的第i個結點為根節點比i-1個結點為根節點小怎麼辦,這樣也會當成是depth==max_depth而被新增到佇列中啊。

void dfs_depth(int u, int depth) {
	vis[u] = true;
	if (depth > max_depth) {
		max_depth = depth;
	}
	for (int i = 0; i < map[u].size(); i++) {
		int v = map[u][i];
		if (vis[v] == false) {
			dfs_depth(v, depth + 1);
		}
	}
}

//以下是mian函式的擷取
if (blot_num == 1) {
		for (int i = 1; i <= N; i++) {
			depth = max_depth;
			init();
			dfs_depth(i, 1);
			if (depth < max_depth) {
				tempOri.clear();
				tempOri.push_back(i);
			}
			else if (depth == max_depth) {
				tempOri.push_back(i);
			}
		}
		for (int i = 0; i < tempOri.size(); i++) {
			printf("%d", tempOri[i]);
			if (i != tempOri.size() - 1) {
				printf("\n");
			}
		}
	}

二,過程二:

    所以關於max_depth變化後的對佇列的操作還是在子函式中進行比較好,這裡為了防止同一個根節點多次遍歷到它的最深葉子節點出現重複入列的現象,故用了set.但出現了執行時間超過規定時間的問題,故以後處理資料較多的情況慎用set.

void dfs_depth(int u, int depth, int pre) {
	if (depth >= max_depth) {
		if (depth > max_depth) {
			tempOri.clear();
			max_depth = depth;
		}
		tempOri.insert(I);
	}
	for (int i = 0; i < map[u].size(); i++) {
		int v = map[u][i];
		if (v != pre) {
			dfs_depth(v, depth + 1, u);
		}
	}
}



if (blot_num == 1) {
		for (I = 1; I <= N; I++) {
//			depth = max_depth;
			dfs_depth(I, 1, -1);
		}
//		sort(tempOri.begin(), tempOri.end());
		for (set<int>::iterator it = tempOri.begin(); it != tempOri.end(); it++) {
			printf("%d\n", *it);
		}
	}

三,過程三:

    不能使用set,所以這裡我用bool vis[max_n]陣列判別是否重複入列,但又出現了記憶體超限,是因為深度過深的緣故。

void dfs_depth(int u, int depth, int pre) {
	if (depth >= max_depth) {
		if (depth > max_depth) {
			ans.clear();
			init();
			max_depth = depth;
		}
		ans.push_back(I);
		flag[I] = true;
	}
	for (int i = 0; i < map[u].size(); i++) {
		int v = map[u][i];
		if (v != pre) {
			dfs_depth(v, depth + 1, u);
		}
	}
}


if (blot_num == 1) {
		for (I = 1; I <= N; I++) {
			//			depth = max_depth;
			dfs_depth(I, 1, -1);
		}
		//		sort(tempOri.begin(), tempOri.end());
		for (int i = 1; i <= N; i++) {
			if (flag[i] == true) {
				printf("%d\n", i);
			}
		}
	}

四,過程四

    為了解決遞迴的次數過多,參考了網上的做法用了廣度優先演算法,明白了對每個結點的層數技術不必用結構體,用一個一維陣列用下標號代表結點號即可表示相應結點號的深度。

#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int max_n = 10010;
int N = 0;
int blot_num = 0;
int max_depth = -1;
int depth[max_n] = { 0 };
int layer[max_n] = { 0 };
int father[max_n] = { 0 };
bool vis[max_n] = { false };
vector<int> Adj[max_n];
queue<int> que;
int findFather(int x) {
	int a = x;
	while (x != father[x]) {
		x = father[x];
	}
	while (a != father[a]) {
		int z = a;
		a = father[a];
		father[z] = x;
	}
	return x;
}
void Union(int x, int y) {
	int faA = findFather(x);
	int faB = findFather(y);
	if (faA != faB) {
		father[faA] = faB;
	}
}
int BFS(int s) {
	int tdepth = 0;
	fill(layer, layer + max_n, 0);
	que.push(s);
	vis[s] = true;
	layer[s] = 1;
	while (!que.empty()) {
		int t = que.front();
		que.pop();
		tdepth = max(tdepth, layer[t]);
		if (tdepth > max_depth) {
			max_depth = tdepth;
		}
		for (int i = 0; i < Adj[t].size(); i++) {
			if (vis[Adj[t][i]] == false) {
				layer[Adj[t][i]] = layer[t] + 1;
				vis[Adj[t][i]] = true;
				que.push(Adj[t][i]);
			}		
		}
	}
	return tdepth;
}
int main() {
	int x = 0, y = 0;
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) {
		father[i] = i;
	}
	for (int i = 0; i < N - 1; i++) {
		scanf("%d %d", &x, &y);
		Adj[x].push_back(y);
		Adj[y].push_back(x);
		Union(x, y);
	}
	for (int i = 1; i <= N; i++) {
		vis[findFather(i)] = true;
	}
	for (int i = 1; i <= N; i++) {
		if (vis[i] == true) {
			blot_num++;
		}
	}
	if (blot_num == 1) {
		for (int i = 1; i <= N; i++) {
			fill(vis, vis + max_n, false);
			depth[i] = BFS(i);
		}
		for (int i = 1; i <= N; i++) {
			if (depth[i] == max_depth) {
				printf("%d\n", i);
			}
		}
	}
	else {
		printf("Error: %d components", blot_num);
	}
	return 0;
}