結果

問題 No.3113 The farthest point
ユーザー tomo8
提出日時 2025-04-19 01:13:19
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 5,224 bytes
コンパイル時間 3,402 ms
コンパイル使用メモリ 293,564 KB
実行使用メモリ 53,792 KB
最終ジャッジ日時 2025-04-19 01:13:29
合計ジャッジ時間 8,482 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef MAIN
#define MAIN
#include __FILE__

int op(int a, int b) {
  return max(a, b);
}

int e() {
  return 0;
}

void _main(){
  cint(n);
  vector<vector<xy>> v(n, vector<xy>(0));
  rep(i, n-1) {
    cint(a, b, c); --a; --b;
    v[a] += {b, c};
    v[b] += {a, c};
  }
  vector<int> dic(n, -1), p(n, -1), dic2(n, -1);
  vector<vector<int>> dpc(n, vector<int>(0));
  vector<Segtree<int, op, e>> dps(n);
  rep(i, n) {
    dpc[i] = vector<int>(v[i].size(), -1);
    dps[i] = Segtree<int, op, e>(v[i].size());
  }
  vector<bool> s(n, 0);
  auto dfs = [&](auto dfs, int arg) -> int {
    s[arg] = 1;
    int ret = 0;
    int i = -1;
    for (auto [k, u] : v[arg]) {
      i++;
      if (s[k]) {
        dic2[arg] = i;
        continue;
      }
      p[k] = arg;
      dic[k] = i;
      ret = max(ret, dpc[arg][i] = dfs(dfs, k) + u);
      dps[arg].set(i, dpc[arg][i]);
    }
    return ret;
  };
  dfs(dfs, 0);
  fill(all(s), 0);
  auto dfs2 = [&](auto dfs2, int arg) -> void {
    s[arg] = 1;
    if (p[arg] != -1) {
      dps[arg].set(dic2[arg], dpc[arg][dic2[arg]] = max(dps[p[arg]].prod(0, dic[arg]), dps[p[arg]].prod(dic[arg]+1, dpc[p[arg]].size())) + v[p[arg]][dic[arg]][1]);
    }
    for (auto [k, u] : v[arg]) {
      if (s[k]) continue;
      dfs2(dfs2, k);
    }
  };
  dfs2(dfs2, 0);
  int ans = 0;
  for (auto& seg : dps) ans = max(ans, seg.all_prod());
  cout << ans << endl;
}

#else
#define MAIN

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using strvec = vector<string>;

constexpr ll mod10 = 1000000007;
constexpr ll mod9 = 998244353;
constexpr ll inf = 10000000000000000;

#define int ll
#define double ld
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repf(i, s, n) for (int i = s; i < (int)(n); i++)
template <class... Args>
void __y_input(Args &...arg)
{
    (cin >> ... >> arg);
}

template <class... Args>
void __y_input_vec_1(int p, Args &...args)
{
    (cin >> ... >> args[p]);
}
template <class Arg>
void __y_input_vec_resize(int size, Arg &arg)
{
    arg.resize(size);
}
template <class... Args>
void __y_input_vec(int n, Args &...args)
{
    (__y_input_vec_resize(n, args), ...);
    rep(i, n)
    {
        (__y_input_vec_1(i, args), ...);
    }
}
#define cint(...)  \
    int __VA_ARGS__; \
    __y_input(__VA_ARGS__)

#define cdbl(...)     \
    double __VA_ARGS__; \
    __y_input(__VA_ARGS__)

#define cstr(...)     \
    string __VA_ARGS__; \
    __y_input(__VA_ARGS__)

#define cvec(n, ...)       \
    vector<int> __VA_ARGS__; \
    __y_input_vec(n, __VA_ARGS__)

#define cvect(t, n, ...) \
    vector<t> __VA_ARGS__; \
    __y_input_vec(n, __VA_ARGS__)

#define last cout << endl
#define yn(bl) (bl ? "Yes" : "No")
#define all(v) v.begin(), v.end()
#define acc(v) accumulate(v.begin(), v.end(), 0LL)
#define nxp(v) next_permutation(v.begin(), v.end())
ostream &yesno(bool bl)
{
    cout << yn(bl);
    return cout;
}
void cyn(bool bl){
    cout << (bl ? "Yes" : "No") << endl;
}
typedef array<int, 2> xy;
typedef array<int, 3> xyz;

template <typename T>
inline void sort(T &vec)
{
    return sort(vec.begin(), vec.end());
}
template <typename T>
inline void rsort(T &vec)
{
    return sort(vec.rbegin(), vec.rend());
}
void _main();

template <typename T>
void prefix_sum(vector<T>& p, vector<T>& s){
  s.emplace_back(p[0]);
  for (size_t i = 1; i < p.size(); i++){
    s.emplace_back(p[i] + s.back());
  }
  return;
}

template <typename T>
void compress(T a, T b){
  vector m(a, b);
  sort(all(m));
  m.erase(unique(all(m)), m.end());
  for (auto itr = a; itr != b; itr++){
    *itr = (lower_bound(all(m), *itr) - m.begin());
  }
}

template <typename T>
using rpq = priority_queue<T, vector<T>, greater<T>>;

template <class T>
void operator+=(vector<T>& v, const T t) {
  v.emplace_back(t);
}

template <class S, S (*op)(S, S), S (*e)()>
struct Segtree {
    std::vector<S> Tree;
    int n;
    int depth = 1, width = 1;
    Segtree(int N = 0) : n(N) {
        while (width < n) depth++, width <<= 1;
        Tree.resize(width * 2);
        for (int i = width; i < width * 2; i++) Tree[i] = e();
        for (int i = width-1; i >= 0; i--) Tree[i] = op(Tree[i<<1], Tree[i<<1|1]);
    }
    Segtree(std::vector<S> v) {
        n = v.size();
        while (width < n) depth++, width <<= 1;
        Tree.resize(width * 2);
        for (int i = width; i < width * 2; i++) Tree[i] = (i < width + n ? v[i - width] : e());
        for (int i = width-1; i >= 0; i--) Tree[i] = op(Tree[i<<1], Tree[i<<1|1]);
    }
    void set(int pos, S val) {
        pos += width;
        Tree[pos] = val;
        for (int c = (pos >> 1); c > 0; c >>= 1) Tree[c] = op(Tree[c<<1], Tree[c<<1|1]);
        return;
    }
    S get(int pos) { return Tree[width + pos]; }
    S all_prod() { return Tree[1]; }
    S prod(int l, int r) {
        S x = e(), y = e();
        l += width, r += width;
        for (; l < r; l >>= 1, r >>= 1) {
            if (l & 1) x = op(x, Tree[l++]);
            if (r & 1) y = op(Tree[--r], y);
        }
        return op(x, y);
    }
};

signed main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    _main();
    return 0;
}

#endif
0