向前走莫回头❤

【hdu 2196】Computer(树形dp)

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6115    Accepted Submission(s): 3086


Problem Description
A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information.


Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
 

Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
 

Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
 

Sample Input
5 1 1 2 1 3 1 1 1
 

Sample Output
3 2 3 4 4
 

Author
scnu
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1561 1011 3456 1520 2242 
 
 [题意][求每个点到其他点的最长路]

【题解】【树形dp】
【一道有点麻烦的dp(有种想要直接叫搜索的冲动),以1为根,先dfs一遍,求出以每个点为根的子树中,权值最大的道路的值和次大值;然后,再dfs,当当前点在其根的最长路上,就用次大值加上当前点和根的路径的权值,若否,则用其根的最大值加上当前点和根的路径的权值,并与当前值比较】
【注意是多组数据】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[20010],nxt[20010],p[10010],val[20010],tot;
int n,len[10010][3];
inline void add(int x,int y,int v)
{
	tot++; a[tot]=y; nxt[tot]=p[x]; p[x]=tot; val[tot]=v;
	tot++; a[tot]=x; nxt[tot]=p[y]; p[y]=tot; val[tot]=v;
}
void dfs(int x,int fa)
{
	for(int i=p[x];i!=-1;i=nxt[i])
	 if(a[i]!=fa)
	  {
	  	dfs(a[i],x);
	  	int s1;
		s1=len[a[i]][0]+val[i]; 
	  	if(len[x][0]<s1) {len[x][1]=len[x][0]; len[x][0]=s1; continue;}
	  	if(len[x][1]<s1) {len[x][1]=s1; continue;}
	  }
}
void find(int x,int fa)
{
	for(int i=p[x];i!=-1;i=nxt[i])
	 if(a[i]!=fa)
	  {
	 	len[a[i]][2]=max(len[x][2],len[a[i]][0]+val[i]==len[x][0]?len[x][1]:len[x][0])+val[i];
	 	find(a[i],x);
	 }
}
int main()
{
	freopen("int.txt","r",stdin);
	freopen("my.txt","w",stdout);
	int i,j;
	while((scanf("%d",&n)==1))
	 {  
	    tot=0;
	 	memset(p,-1,sizeof(p));
	    memset(nxt,-1,sizeof(nxt));
	    memset(len,0,sizeof(len));
	    for(i=2;i<=n;++i)
	     {
	 	    int x,z;
	 	    scanf("%d%d",&x,&z);
	 	    add(i,x,z);
	    }
	    dfs(1,1); 
		len[1][2]=0;
	    find(1,1);
	    for(i=1;i<=n;++i) printf("%d\n",max(len[i][0],len[i][2]));
	 }
	return 0;
}

[附数据生成器]
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int f[10010];
int find(int x)
{
	if(f[x]==x) return x;
	f[x]=find(f[x]);
	return f[x];
}
int main()
{
	freopen("int.txt","w",stdout);
	srand(time(0));
	int T=rand()%100+1;
	while(T--)
	 {
	 	int n=rand()%10000+1;
	    printf("%d\n",n);
	    for(int i=1;i<=n;++i) f[i]=i;
	    for(int i=2;i<=n;++i)
	     {
	 	    int x=rand()%n+1;
	 	    int val=rand()%10000+1;
	 	    while(find(x)==find(i)) x=rand()%n+1;
	 	    int l1=find(x),l2=find(i);
	 	    f[l1]=l2;
	 	    printf("%d %d\n",x,val);
	    }
	 }
}



posted @ 2016-09-30 17:34  lris0-0  阅读(61)  评论(0编辑  收藏  举报
过去的终会化为美满的财富~o( =∩ω∩= )m