Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 906 Accepted Submission(s): 268
Problem Description
In the Data structure class of HEU, the teacher asks one problem: How to find the longest path of one tree and the number of such longest path?
Input
There are several test cases. The first line of each case contains only one integer N, means there are N nodes in the tree. N-1 lines follow, each line has three integers w,v and len, indicate that there is one edge between node w and v., and the length of
the edge is len.
the edge is len.
Output
For each test case, output the length of longest path and its number in one line.
Sample Input
4
1 2 100
2 3 50
2 4 50
4
1 2 100
2 3 50
3 4 50
1 2 100
2 3 50
2 4 50
4
1 2 100
2 3 50
3 4 50
Sample Output
150 2
200 1
200 1
Source
题意:给出一颗树(n个顶点,n-1条边)
求最长的路径,以及最长路径的条数。
路径无非就是连接两个点直接的路。
因为是一颗树,所以连接两个点肯定是唯一的路径。
其实就是求两点间距离的最大值,以及这个最大值有多少个。
首先统计出结点到叶子结点的最长距离和次长距离。
然后找寻经过这个点的,在这个为根结点的子树中的最长路径个数目。
...思路参考于kuangbin博客
代码:
#include "stdio.h" //poj 3534 Tree 树形dp
#include "string.h" #define N 100100 struct node
{
int x,y;
int weight;
int next;
}edge[*N];
int idx,head[N]; void Init()
{
idx = ;
memset(head,-,sizeof(head));
} void Add(int x,int y,int weight)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].weight = weight;
edge[idx].next = head[x];
head[x] = idx++;
} int maxn[N]; //最长距离
int smaxn[N];//次长距离
int maxn_num[N]; //最长距离数目
int smaxn_num[N]; //次长距离数目
int path[N]; //经过点i的最长距离长度
int num[N]; //经过点i的最长距离数目 void DFS(int x,int father)
{
int i,y;
maxn[x] = smaxn[x] = ;
maxn_num[x] = smaxn_num[x] = ;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
DFS(y,x);
if(maxn[x] < maxn[y]+edge[i].weight)
{
smaxn[x] = maxn[x];
smaxn_num[x] = maxn_num[x];
maxn[x] = maxn[y]+edge[i].weight;
maxn_num[x] = maxn_num[y];
}
else if(maxn[x]==maxn[y]+edge[i].weight)
maxn_num[x] += maxn_num[y];
else if(smaxn[x] < maxn[y]+edge[i].weight)
{
smaxn[x] = maxn[y]+edge[i].weight;
smaxn_num[x] = maxn_num[y];
}
else if(smaxn[x] == maxn[y]+edge[i].weight)
smaxn_num[x] += maxn_num[y];
}
if(maxn_num[x]==) //叶子节点,赋初值
{
maxn[x] = smaxn[x] = ;
maxn_num[x] = smaxn_num[x] = ;
path[x] = ;
num[x] = ;
return ;
}
int c1; //统计以x为根节点的最长距离数目
int c2; //统计以x为根节点的次长距离数目
c1 = c2 = ;
for(i=head[x]; i!=-; i=edge[i].next)
{
y=edge[i].y;
if(y==father) continue;
if(maxn[x] == maxn[y]+edge[i].weight)
c1++;
else if(smaxn[x] == maxn[y]+edge[i].weight)
c2++;
}
path[x] = ;
num[x] = ;
if(c1>=)
{
int tmp = ;
path[x] = maxn[x]*;
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
if(maxn[x]==maxn[y]+edge[i].weight)
{
num[x] += tmp*maxn_num[y];
tmp += maxn_num[y];
}
}
}
else if(c1>= && c2>=)
{
path[x] = maxn[x]+smaxn[x];
for(i=head[x]; i!=-; i=edge[i].next)
{
y = edge[i].y;
if(y==father) continue;
if(maxn[x]==maxn[y]+edge[i].weight)
{
num[x] = smaxn_num[x]*maxn_num[y];
}
}
}
else
{
path[x] = maxn[x];
num[x] = maxn_num[x];
}
} int main()
{
int n;
int i,j;
int x,y,k;
while(scanf("%d",&n)!=EOF)
{
Init();
for(i=; i<n; ++i)
{
scanf("%d %d %d",&x,&y,&k);
Add(x,y,k);
Add(y,x,k);
}
DFS(,-);
int dist=,snum=;
for(i=; i<=n; ++i)
{
if(path[i]>dist)
{
dist = path[i];
snum = num[i];
}
else if(path[i]==dist)
{
snum += num[i];
}
}
printf("%d %d\n",dist,snum);
}
return ;
}