HDU 2196 Computer [树的直径]

Computer
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

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


求出任意点的最远点 O(n)

第一次从任意点dfs找出最远点 end1 ,第二次从end1开始dfs找出其最远点 end2 , 同时边走边更新每个点的最远点 (因为每个点的最远点只可能是end1 或者 end2),然后第三次从end2开始dfs更新答案即可。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<iomanip>
#include<ctime>
#include<climits>
#include<cctype>
#include<algorithm>
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
#define smax(x,tmp) x=max((x),(tmp))
#define smin(x,tmp) x=min((x),(tmp))
#define maxx(x1,x2,x3) max(max(x1,x2),x3)
#define minn(x1,x2,x3) min(min(x1,x2),x3)
const int INF=0x3f3f3f3f;
const int maxn = 10005;
struct Edge
{
    int to,next;
    int val;
}edge[maxn<<1];
int head[maxn];
int maxedge;
inline void addedge(int u,int v,int c)
{
    edge[++maxedge] = (Edge) { v,head[u],c };
    head[u] = maxedge;
    edge[++maxedge] = (Edge) { u,head[v],c };
    head[v] = maxedge;
}
int n;
int end1,end2,tmp;
int dis[maxn];
inline bool init()
{
    if(!~scanf("%d",&n)) return false;
    memset(head,-1,sizeof(head));
    maxedge=-1;
    for(int i=2;i<=n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        addedge(x,i,y);
    }
    return true;
}
void dfs1(int u,int father,int d)
{
    if(tmp<d) tmp=d,end1=u;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(v==father) continue;
        dfs1(v,u,d+edge[i].val);
    }
}
void dfs2(int u,int father,int d)
{
    smax(dis[u],d);
    if(tmp<d) tmp=d,end2=u;
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(v==father) continue;
        dfs2(v,u,d+edge[i].val);
    }
}
void dfs3(int u,int father,int d)
{
    smax(dis[u],d);
    for(int i=head[u];~i;i=edge[i].next)
    {
        int v = edge[i].to;
        if(v==father) continue;
        dfs3(v,u,d+edge[i].val);
    }
}
void work()
{
    memset(dis,0,sizeof(dis));
    tmp=0;
    dfs1(1,-1,0);
    tmp=0;
    dfs2(end1,-1,0);
    tmp=0;
    dfs3(end2,-1,0);
    for(int i=1;i<=n;i++) printf("%d\n",dis[i]);
}
int main()
{
    freopen("computer.in","r",stdin);
    freopen("computer.out","w",stdout);
    while(init())
        work();
    return 0;
}
基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值