結果

問題 No.3113 The farthest point
ユーザー The Forsaking
提出日時 2025-04-18 22:24:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,076 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 198,004 KB
実行使用メモリ 29,496 KB
最終ジャッジ日時 2025-04-18 22:24:26
合計ジャッジ時間 5,357 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:35:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |         scanf("%d%d%d", &a, &b, &c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef pair<long long, int> pli;
typedef pair<int, pair<int, int> > piii;
typedef long long ll;
const int N = 2000086, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
ll res;
int n, m, cnt, w[N];
int e[N], ne[N], h[N], idx, t[N];
void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++; }

vector<ll> f[200086];

void dfs(int r, int fa) {
    for (int i = h[r]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == fa) continue;
        dfs(j, r);
        f[r].push_back(max(0ll, f[j].back()) + w[i]);
        res = max(res, max(0ll, f[j].back()) + w[i]);
    }
    sort(f[r].begin(), f[r].end());
    if (f[r].size() > 1) res = max(res, f[r].back() + f[r][f[r].size() - 2]);
    if (!f[r].size()) f[r].push_back(0);
}

int main() {
    cin >> n;
    memset(h, -1, sizeof(int) * (n + 10));
    for (int i = 1; i < n; i++) {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c), add(b, a, c);
    }
    dfs(1, -1);
    printf("%lld\n", res);
    return 0;
}
0