50pts|求大佬帮忙看看555
查看原帖
50pts|求大佬帮忙看看555
429818
Smithespics楼主2023/3/21 20:13
using namespace std;
struct Node
{
    int to,next,cost;
}node[100005];
int n,u,v,w,cnt,root;
int head[200005];
int visit[100005];
int dist[100005];
int maxlen;

int Calc(int x)
{
    return 10*x + x*(x+1)/2;
}

void add(int u,int v,int w)
{
    node[cnt].to   = v;
    node[cnt].next = head[u];
    node[cnt].cost = w;
    head[u]        = cnt++;
}

void dfs(int x,int fa)
{
    int max1 = 0,max2 = 0;
    for(int i = head[x];~i;i = node[i].next)
    {
        int to = node[i].to;
        if(!visit[to] && to != fa)
        {
            visit[to] = 1;
            dfs(to,x);
            dist[i] = dist[to] + node[i].cost;
        }
        if(dist[i] > max1)
        {
            max2 = max1;
            max1 = dist[i];
        }
        else if(dist[i] <= max1 && dist[i] > max2)

            max2 = dist[i];
    }
    dist[x] = max1;
    maxlen = max(maxlen,max1+max2);
}

int main()
{
    memset(head,-1,sizeof(head));
    cin >> n;
    for(int i = 1;i < n;i++)
    {
        cin >> u >> v >> w;
        add(u,v,w);
        add(v,u,w);
    }

    for(int i = 1;i <= n;i++)
        if(~node[head[root = i]].next && !visit[root])
            visit[root] = 1,dfs(root,0);
    cout << Calc(maxlen);
    return 0;
}
2023/3/21 20:13
加载中...