时间限制: 2000 ms 内存限制: 64 MB 代码长度限制: 16 KB
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 (≤104) 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
题目大意:
acyclic 无环的;非循环的。
非循环的图可以看做树,树的高度取决于所选择的根节点。现给你一棵树,找出有最高高度的根节点。
输入样例第一行给出节点个数N,随后N-1行给出边(因为是非循环)。(N<=10000)
输出样例,输出具有最深高度的根节点,如果不唯一按数字从小到大输出;如果给出图不是单一连通集,输出“Error: K components”,其中K是连通集个数。
题目分析:
求深度,想到深度优先搜索。把把每一个点当做起点遍历一遍,找到最大的那个点和他的高度。
最大点的表示:用vector<Node>的形式表示出来,一个一个添加,最后排序并输出。
如果遍历结束所涉及节点个数小于总个数,则找出有几个连通集。
重要部分:
注意事项:
完整代码:
提交时间 | 状态 | 分数 | 题目 | 编译器 | 耗时 | 用户 |
---|---|---|---|---|---|---|
2018/11/28 00:10:28 | 部分正确 |
20 | 1021 | C++ (g++) | 1109 ms | Dirichlet |
测试点 | 结果 | 耗时 | 内存 |
---|---|---|---|
0 | 答案正确 | 3 ms | 512KB |
1 | 答案正确 | 3 ms | 640KB |
2 | 答案错误 | 4 ms | 512KB |
3 | 答案正确 | 1109 ms | 1152KB |
4 | 答案正确 | 3 ms | 640KB |
5 | 答案正确 | 3 ms | 768KB |
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
struct Node{
int data;
int height;
};
const int maxn = 10010;
vector<int> Adj[maxn]; //图的邻接表
vector<Node> H; //表示树高的数组
bool vis[maxn]={false};
int N;//节点个数
int depth, numConnect, deepest=0;//每次计算的高度和连通集内节点个数
void DFS(int u, int d){//节点,深度
numConnect++;//记录连接点个数增加
vis[u]=true;
for(int i=0; i<Adj[u].size(); i++){//第一维从1开始,第二维从0开始
int v = Adj[u][i];
if(vis[v]==false){
DFS(v,d+1);
}
deepest = deepest>d?deepest:d;
}
}
//传统遍历求连通集个数,返回负值
int DFSReal(){
memset(vis,false,sizeof(vis));
int numCircle=0;
for(int i=1; i<N; i++){
if(vis[i]==false){
DFS(i,1);
numCircle++;
}
}
return -numCircle;
}
//以u为起点,输出以u为根的高度,如果不连通输出负值表示连通集个数
int DFSTraversal(int u){
//初始化
numConnect=0;
deepest=0;
memset(vis, false, sizeof(vis));
//求树高
DFS(u,1);
if(numConnect<N){
return DFSReal();
}
return deepest;
}
bool comp(Node a, Node b){
if(a.height!=b.height) return a.height>b.height;
else return a.data<b.data;
}
int main()
{
int e1,e2;
cin>>N;
for(int i=1; i<N; i++){
Node tmp1,tmp2;
cin>>e1>>e2;
Adj[e1].push_back(e2);
Adj[e2].push_back(e1);
}
//processing
Node c;
for(int i=1; i<=N; i++){
c.data=i;
c.height=DFSTraversal(i);
if(c.height<0){//不连通,结束,输出
cout<<"Error: "<<-c.height<<" components"<<endl;
return 0;
}
H.push_back(c);
}
//sorting
sort(H.begin(), H.end(), comp);
//outputing
for(int i=0; i<N && H[i].height==H[0].height; i++){
cout<<H[i].data<<endl;
}
return 0;
}