---
id: 70
lang: en
translation_of: index.md
source_sha256: ed868a7d98337dd7e5621ac030a3cf2b9ad039af68fb336317f5ee119c46af7a
title: "P1351: Joint Weight — Solution"
description: "I originally wrote this problem with brute-force search (DFS), but it got TLE on 3 test cases."
tags: ["P1351","Joint Weight","Math","Hacky Heuristics","Graph Theory"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "“获取与点n相连的点” kept literally as “adjacent to vertex n” (the author means the current vertex)."
  - "%%% chipped as one span with the glossary gloss for %, which covers the repeated form."
---

## Approach

I originally wrote this problem with brute-force search (DFS), but it got TLE on 3 test cases. I had no choice but to go ask lornd, <span class="slang" data-zh="巨佬" data-gloss="jùlǎo, “huge guy”. A very skilled contestant; used with admiration.">a big shot</span> in our computer lab, for help. Under his magical moves, this problem went from $O(n^3)$ to $O(n)$ time complexity.

Let's first break down our procedure. After storing the graph, we need to iterate over the n vertices and then get the vertices adjacent to vertex n. The distance between these vertices must be 2. Next we process them.

### Question 1

While iterating we record the largest and the second-largest numbers. After processing these vertices, we multiply the **largest and second-largest** numbers, which gives the largest joint weight among these ordered pairs; taking the max with the ans variable solves question 1.

### Question 2

Let's first write down a formula:
$$
(\sum^{n}_{i=0}{a_i})^2 = \sum^{n}_{i=0}{a^2_i} + 2(\sum^{m}_{i=0,j=0}{a_i a_j})
$$

What question 2 actually asks for is the third term of this formula:
$$
2(\sum^{m}_{i=0,j=0}{a_i a_j})
$$

That is, $2ab + 2cd + 2ef + \dots$ So if we want to find it, we can compute the first and second terms in the code and subtract them to get the answer.

## Code

```cpp
// P1351.cpp
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

#define ull unsigned long long
const int maxn = 2000000;
int n;
ull W[maxn];
// The graph data structure;
int to[maxn];
int point[maxn];
int next[maxn];
bool vis[maxn];
int current = 0;
// answers;
ull ans_max = 0;
ull ans_tot = 0;
// initialize to make the graph up;
void init()
{
    memset(to, -1, sizeof(to));
    memset(point, -1, sizeof(point));
    memset(next, -1, sizeof(next));
    memset(vis, false, sizeof(vis));
    cin >> n;

    for (int i = 0; i < n - 1; i++)
    {
        int x, y;
        cin >> x >> y;
        next[current] = point[x];
        to[current] = y;
        point[x] = current;
        current++;
        next[current] = point[y];
        to[current] = x;
        point[y] = current;
        current++;
    }
    for (int i = 1; i <= n; i++)
        cin >> W[i];
}
// calculate the n^2;
ull secondary(ull a)
{
    return a * a;
}
// solve;
void solve()
{
    for (int i = 1; i <= n; i++)
    {
        // get the maximum and the second one;
        ull firmax = 0;
        ull secmax = 0;
        // the term1 and the term2;
        ull tmp1 = 0;
        ull tmp2 = 0;
        for (int j = point[i]; j != -1; j = next[j])
        {
            int jto = to[j];
            if (W[jto] > firmax)
                secmax = firmax, firmax = W[jto];
            else if (W[jto] > secmax)
                secmax = W[jto];
            tmp1 += W[jto];
            tmp2 += secondary(W[jto]);
        }
        // add them up;
        ans_tot += (secondary(tmp1) - tmp2) % 10007;
        ans_tot %= 10007;
        ans_max = max(ans_max, firmax * secmax);
    }
}
// just solve it;
int main()
{
    init();
    solve();
    cout << ans_max << " " << ans_tot;
    return 0;
}
```

lornd <span class="slang" data-zh="tql" data-gloss="Pinyin initials of 太强了 (tài qiáng le), “too strong”. Praise for someone's skill.">so strong</span> <span class="slang" data-zh="%%%" data-gloss="Read as 膜 (mó, “worship”), because % is “mod” and sounds like mó. Repeated as %%% to heap praise on someone.">all hail</span>
