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
解析在代码注释里
代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
class EdgeNode
{
public:
int adjvex;
EdgeNode* next;
int weight;
EdgeNode() :adjvex(-1), next(nullptr), weight(1) {}
EdgeNode(int adj) :adjvex(adj), next(nullptr), weight(1) {}
EdgeNode(int adj, EdgeNode* e, int weight,) : adjvex(adj), next(e), weight(weight){}
};
class VertexNode
{
public:
EdgeNode* firstarc;
};
class DeepestRoot
{
private:
int nodes;
vector<VertexNode> matrix;
vector<int> visited;
vector<int> deep_root;
bool isTree;
int max_deep;
public:
DeepestRoot()
{
input();
isTree = true;
max_deep = 0;
for (int i = 1; i <= nodes; ++i)
{
visited.clear();
visited.resize(nodes + 1, 0);
visited[0] = -1;
int depth = 0;
BFS(i, depth);
if (depth > max_deep)
max_deep = depth;
if (find(visited.begin(), visited.end(), 0) != visited.end())
{
//说明此图不连通,转去执行计算连通分量代码
isTree = false;
countcon();
return;
}
deep_root.push_back(depth);
}
print();
}
void input()
{
scanf("%d", &nodes);
matrix.resize(nodes + 1);
for (int i = 0; i < nodes - 1; ++i)
{
int row, line;
scanf("%d %d", &row, &line);
EdgeNode* e1 = new EdgeNode(line, matrix[row].firstarc, 1, 0);
matrix[row].firstarc = e1;
EdgeNode* e2 = new EdgeNode(row, matrix[line].firstarc, 1, 0);
matrix[line].firstarc = e2;
}
}
void BFS(int v, int& depth)
{
queue<int> que;
que.push(v);
visited[v] = 1;
while (!que.empty())
{
int k = que.front();
que.pop();
depth = visited[k];
EdgeNode* e = matrix[k].firstarc;
while (e)
{
if (!visited[e->adjvex])
{
que.push(e->adjvex);
visited[e->adjvex] = depth + 1;
}
e = e->next;
}
}
}
void countcon()
{
visited.clear();
visited.resize(nodes + 1, 0);
visited[0] = -1;
int count = 0;
for (int i = 1; i < nodes + 1; ++i)
{
if (!visited[i])
{
bfs(i);
++count;
}
}
if (1 == count) printf("Error: %d component", count);
else printf("Error: %d components", count);
}
void bfs(int v)
{
queue<int> que;
que.push(v);
visited[v] = 1;
while (!que.empty())
{
int k = que.front();
que.pop();
EdgeNode* e = matrix[k].firstarc;
while (e)
{
if (!visited[e->adjvex])
{
que.push(e->adjvex);
visited[e->adjvex] = 1;
}
e = e->next;
}
}
}
void print()
{
int max = 0;
for (size_t i = 0; i < deep_root.size(); ++i)
{
if (deep_root[i] == max_deep)
printf("%d\n", i + 1);
}
}
};
int main()
{
DeepestRoot dp;
return 0;
}