1021. Deepest Root (25)
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 5Sample Output 1:
3 4 5Sample Input 2:
5 1 3 1 4 2 5 3 4Sample Output 2:
Error: 2 components
分析:bfs遍历树,并计入他的高度;如果树不为连通的话,记录连通分量的个数;
code:存在一个段错误,暂时还没有发现;而且二维数组不能开到10000*10000,因为会内存超限;
#include <stdio.h>
#include <queue>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
queue<int> q1;
queue<int> q2;
int n;
int visit[10000]={0};
int edge[2000][2000];
int bfs(int v)
{
int height=0;
int temp;
visit[v]=1;
if(v>n||v<=0) return -1;
q1.push(v);
while(!q1.empty())
{
temp=q1.front();
if(!temp) break;
for(int i=1;i<=n;i++)
{
if(edge[temp][i]!=0&&visit[i]==0)
{
q2.push(i);
visit[i]=1;
}
}
q1.pop();
if(q1.empty()&&!q2.empty())
{
height++;
while(!q2.empty())
{
int r1=q2.front();
q1.push(r1);
q2.pop();
}
}
}
return height;
}
int main()
{
scanf("%d",&n);
memset(edge,0,sizeof(edge));
int h[10000];
for(int i=0;i<n-1;i++)
{
int k,m;
scanf("%d %d",&k,&m);
edge[k][m]=edge[m][k]=1;
}
int flag;
for(int i=1;i<=n;i++)
{
memset(visit,0,sizeof(visit));
h[i]=bfs(i);
}
//先算出每个root 的高度
memset(visit,0,sizeof(visit));
int liantongtu=1;
for(int i=1;i<=n;i++)
{
flag=0;
if(visit[i]==0)
{
bfs(i);
for(int j=1;j<=n;j++)
{
if(visit[j]==0) {
flag=1;break;}
}
if(flag==1) liantongtu++;
}
}
if(liantongtu==1)
{
int maxdepth=h[1];
vector<int> depth;
for(int i=1;i<=n;i++)
{
if(maxdepth<h[i])
maxdepth=h[i];
}
for(int i=1;i<=n;i++)
{
if(h[i]==maxdepth)
depth.push_back(i);
}
for(vector<int>::iterator i=depth.begin();i<depth.end();i++)
{
printf("%d\n",*i);
}
}
else
printf("Error: %d components",liantongtu);
return 0;
}
下面是ac code
#include<iostream>
#include<stdio.h>
#include<stack>
#include<vector>
#include<stack>
#include<queue>
#include<string.h>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int dist[10010];
vector<int>adj_list[10010];
int P[10010];
struct Number
{
int point;
int dis;
};
vector<Number>num;
//return the max distance
int bfs(int start , int V)
{
memset(dist,-1,sizeof(dist));
dist[start] = 0;
queue<int> q;
q.push(start);
int dmax = 0;
while(!q.empty())
{
int now = q.front();
q.pop();
int d = dist[now];
for(int i = 0; i < adj_list[now].size(); i++)
{
//not visit before
if(dist[ adj_list[now][i] ] == -1)
{
dist[ adj_list[now][i] ] = d +1;
q.push(adj_list[now][i]);
}
}
if(d > dmax)
{
dmax = d;
}
}
return dmax;
}
bool cmp1(Number a,Number b)
{
if(a.dis==b.dis)
return a.point<b.point;
return a.dis>b.dis;
}
bool cmp2(Number a,Number b)
{
return a.point<b.point;
}
int Union_Find(int id)
{
if(P[id]==-1)
return id;
else
return Union_Find(P[id]);
}
void Union_Union(int a,int b)
{
int id1=Union_Find(a);
int id2=Union_Find(b);
if(id1!=id2)
P[id1]=id2;
}
int main()
{
//freopen("D://test.txt","r" , stdin);
int i;
int a,b,n;
scanf("%d",&n);
memset(P,-1,sizeof(P));
for(i=0;i<n-1;i++)
{
scanf("%d%d",&a,&b);
Union_Union(a,b);
adj_list[a].push_back(b);
adj_list[b].push_back(a);
}
int count=0;
for(i=1;i<=n;i++)
{
if(P[i]==-1)
count++;
}
if(count!=1)
printf("Error: %d components",count);
else
{
int temp;
num.clear();
Number buf;
for(i=1;i<=n;i++)
{
buf.point=i;
temp=bfs(i,n);
buf.dis=temp;
num.push_back(buf);
}
sort(num.begin(),num.end(),cmp1);
int pos;
for(i=0;i<n;i++)
{
if(i!=n-1)
{
if(num[i].dis!=num[i+1].dis)
{
pos=i;
break;
}
}
else
{
pos=i;
}
}
//sort(num.begin(),num.begin()+pos,cmp2);
for(i=0;i<=pos;i++)
{
if(i!=pos)
printf("%d\n",num[i].point);
else
printf("%d",num[i].point);
}
}
return 0;
}