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.
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<cstdio>
#include<cmath>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
#include<queue>
const int N = 10005;
const int INF = 1000000000;
using namespace std;
bool vis[N] ;
vector<int> adj[N],ans;
int n,m,k,maxx,flag;
void iscycle(int s){
vis[s] = true;
for(int i=0;i<adj[s].size();i++){
if(vis[adj[s][i]] == false){
iscycle(adj[s][i]);
}
}
}
void dfs(int s,int bk){
vis[s] = true;
if(bk > maxx) maxx = bk;
for(int i=0;i<adj[s].size();i++){
if(vis[ adj[s][i] ] == false){
dfs(adj[s][i],bk+1);
}
}
}
void dfs1(int s,int bk){
vis[s] = true;
if(bk == maxx) flag = 1 ;
for(int i=0;i<adj[s].size();i++){
if(vis[adj[s][i]] == false){
dfs1(adj[s][i],bk+1);
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int num = 0;
for(int i=1;i<=n;i++){
if(vis[i] == false){
num++;
iscycle(i);
}
}
if(num > 1){
printf("Error: %d components",num);
return 0;
}
for(int i=1;i<=n;i++){
memset(vis,false,sizeof(vis));
dfs(i,0);
}
for(int i=1;i<=n;i++){
memset(vis,false,sizeof(vis));
flag = 0;
dfs1(i,0);
if(flag == 1) ans.push_back(i);
}
sort(ans.begin(),ans.end());
for(int i=0;i<ans.size();i++){
cout<<ans[i]<<endl;
}
return 0;
}
第二次 23分,有一点超时,
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
const int N = 10005;
const int INF = 1000000000;
using namespace std;
bool vis[N] ;
vector<int> adj[N];
set<int> ans;
int n,m,k,maxx,flag;
void iscycle(int s){
vis[s] = true;
for(int i=0;i<adj[s].size();i++){
if(vis[adj[s][i]] == false){
iscycle(adj[s][i]);
}
}
}
void dfs(int s,int bk,int origin){
vis[s] = true;
if(bk > maxx) {
maxx = bk;
ans.clear();
ans.insert(origin);
}
else if( bk == maxx) ans.insert(origin);
for(int i=0;i<adj[s].size();i++){
if(vis[ adj[s][i] ] == false){
dfs(adj[s][i],bk+1,origin);
}
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n-1;i++){
int a,b;
scanf("%d%d",&a,&b);
adj[a].push_back(b);
adj[b].push_back(a);
}
int num = 0;
for(int i=1;i<=n;i++){
if(vis[i] == false){
num++;
iscycle(i);
}
}
if(num > 1){
printf("Error: %d components",num);
return 0;
}
for(int i=1;i<=n;i++){
memset(vis,false,sizeof(vis));
dfs(i,0,i);
}
set<int>::iterator it;
for(it = ans.begin();it!=ans.end();it++){
printf("%d\n",*it);
}
return 0;
}
优化的地方就是在遍历的时候把符合题目的点用set记录下来,以前也是用过这种思想,但是再做这道题的时候并没有想到,这种思想还是比较好的,一般再最短路径上用到,我是按照目录来的所以还没复习到 = =
870

被折叠的 条评论
为什么被折叠?



