1021 Deepest Root
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 (≤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
还是想不出来(只想得到思路,不会写代码!)
基本思路:
首先用类似于邻接链表的vector存储两个点之间的边,temp的作用是存储从当前点出发到达最远的点的编号,先求出从第一个点能到达的最远的点(可能有多个),然后利用s1存储其中一个即可,然后重新dfs遍历,这次从s1开始,一直到最后远的点,如果有多个就全部都加入temp中,最后全部存进set集合中(会自动排序),然后打印即可
看图理解:
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
using namespace std;
vector<int> temp;
vector<vector<int>> v(10010);
bool st[10010];
int height,n;
void dfs(int t,int depth){
if(depth>height){
temp.clear();
temp.push_back(t);
height=depth;
}
else if(depth==height) temp.push_back(t);
st[t]=true;
for(int i=0;i<v[t].size();i++)
if(!st[v[t][i]]) dfs(v[t][i],depth+1);
}
int main(){
scanf("%d",&n);
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);
}
set<int> s;
int cot=0,s1=0;
for(int i=1;i<=n;i++){
if(!st[i]){
dfs(i,1);
if(temp.size()) s1=temp[0];//这个点是从点1开始能走到的最远的点
for(int i=0;i<temp.size();i++)
s.insert(temp[i]);
cot++;
}
}
if(cot!=1) printf("Error: %d components\n",cot);
else{
memset(st,false,sizeof st);
dfs(s1,1);//从这个点开始看能有哪些点能走到最远
for(int i=0;i<temp.size();i++)
s.insert(temp[i]);
for(auto it=s.begin();it!=s.end();it++)
printf("%d\n",*it);
}
return 0;
}
好好学习,天天向上!
我要考研!
2022.11.1
//基本思路:
//判断这颗树是否是连通树,如果不是的话则得出结果
//如果是的话,遍历所有的点,查看该点dfs能走到的最远的距离,中间有一个需要注意的点
//就是如果有两个点走到同一个重点都是最远距离的话,终点会添加两次,所以需要一个哈希表来存放
//当前已经添加的点
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
vector<vector<int>> v(10010);
bool st[10010];
int maxn;
int h[10010];
vector<int> ans;
void dfs(int t){
st[t]=true;
for(int i=0;i<v[t].size();i++)
if(!st[v[t][i]]) dfs(v[t][i]);
}
void dfs2(int t,int depth){
st[t]=true;
if(maxn<depth){
ans.clear();
memset(h,0,sizeof h);
ans.push_back(t);
h[t]=1;
maxn=depth;
}
else if(maxn==depth){
if(!h[t]){
ans.push_back(t);
h[t]=1;
}
}
for(int i=0;i<v[t].size();i++)
if(!st[v[t][i]]) dfs2(v[t][i],depth+1);
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n-1;i++){
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
int cot=0;
for(int i=1;i<=n;i++){
if(!st[i]){
cot++;
dfs(i);
}
}
if(cot!=1) printf("Error: %d components\n",cot);
else{
//此处有一个循环和一个递归,耗时会长很多
for(int i=1;i<=n;i++){
memset(st,false,sizeof st);
dfs2(i,0);
}
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++)
printf("%d\n",ans[i]);
}
return 0;
}