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 (≤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.
1 | Sample Input 1: |
思路:求树的直径
要能处理特殊数据 n=1
不能走回头路 要记录前驱节点1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define MAX 10001
#define INF 0x3f3f3f
using namespace std;
int par[MAX];
int maxn=-1;
vector<int> vec[MAX],ans,tmp;
bool isR[MAX];
int find(int a){
if(par[a]!=a) par[a]=find(par[a]);
return par[a];
}
bool Union(int a,int b){
int r1=find(a);
int r2=find(b);
if(r1==r2) return false;
//else par[a]=b;
par[r1]=r2;//!!
return true;
}
void dfs(int cur,int hei,int pre){
if(hei>maxn){
maxn=hei;
tmp.clear();
tmp.push_back(cur);
}
if(hei==maxn){
tmp.push_back(cur);
}
for(int i=0;i<vec[cur].size();i++){
int v=vec[cur][i];
if(v==pre) continue;
dfs(v,hei+1,cur);
}
}
int main(){
// freopen("3-18.txt","r",stdin);
int n,a,b;
cin>>n;
for(int i=1;i<=n;i++)
par[i]=i;
for(int i=0;i<n-1;i++){
cin>>a>>b;
vec[a].push_back(b);
vec[b].push_back(a);
Union(a,b);
}
for(int i=1;i<=n;i++){
isR[find(i)]=true;
}
int sum=0;
for(int i=1;i<=n;i++){
if(isR[i]==true) sum++;
}
if(sum!=1){
cout<<"Error: "<<sum<<" components"<<endl;
return 0;
}
dfs(1,1,-1);
ans=tmp;
tmp.clear();
dfs(ans[0],1,-1);
for(int i=0;i<tmp.size();i++){
ans.push_back(tmp[i]);
}
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++){
if(ans[i]!=ans[i+1])
cout<<ans[i]<<endl;
}
}
20分代码 用了ans1,ans,vis,dis 内存超限 应该考虑dfs时带参数,直接把最远距离的点放入ans中1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102#include<iostream>
#include<set>
#include<cstring>
#include<vector>
#define MAX 10001
#define INF 0x3f3f3f
using namespace std;
int par[MAX];
vector<int> vec[MAX],ans1,ans2;
bool vis[MAX];
int dis[MAX];
int find(int a){
if(par[a]!=a) par[a]=find(par[a]);
return par[a];
}
bool Union(int a,int b){
int r1=find(a);
int r2=find(b);
if(r1==r2) return false;
//else par[a]=b;
par[r1]=r2;//!!
return true;
}
void dfs(int cur){
// if(vis[cur]) return ;
vis[cur]=true;
for(int i=0;i<vec[cur].size();i++){
int v=vec[cur][i];
if(vis[v]) continue;
dis[v]=dis[cur]+1;
dfs(v);
}
}
int main(){
set<int> s;
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
int n,a,b;
cin>>n;
for(int i=1;i<=n;i++)
par[i]=i;
for(int i=0;i<n-1;i++){
cin>>a>>b;
vec[a].push_back(b);
vec[b].push_back(a);
Union(a,b);
// Union(b,a);
}
int r=find(1);
s.insert(r);
bool flag=true;
for(int i=2;i<=n;i++){
int t=find(i);
if(t!=r) {
flag=false;
}
s.insert(t);
}
if(flag==false) {
cout<<"Error: "<<s.size()<<" components"<<endl;
return 0;
}
dfs(1);
// for(int i=1;i<=n;i++)
// cout<<dis[i]<<" ";
// cout<<endl;
int maxn=-1;
for(int i=2;i<=n;i++){
if(dis[i]>maxn) {
ans1.clear();
maxn=dis[i];ans1.push_back(i);
}
else if(dis[i]==maxn){
ans1.push_back(i);
}
}
for(int k=0;k<ans1.size();k++){
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
dfs(ans1[k]);
for(int i=2;i<=n;i++){
if(dis[i]>maxn) {
ans2.clear();
maxn=dis[i];ans2.push_back(i);
}
else if(dis[i]==maxn){
ans2.push_back(i);
}
}
}
s.clear();
for(int i=0;i<ans1.size();i++){
s.insert(ans1[i]);
}
for(int i=0;i<ans2.size();i++){
s.insert(ans2[i]);
}
for(set<int>::iterator it=s.begin();it!=s.end();it++)
cout<<*it<<endl;
}