alexjercan/bugnet huggingface dataset:
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

namespace rika {
template <typename T> class edge {
public:
  int to;
  T cost;

  edge() = default;
  edge(const edge<T> &) = default;
  edge(edge<T> &&) = default;
  edge &operator=(const edge<T> &) = default;
  edge &operator=(edge<T> &&) = default;

  ~edge() = default;

  edge(int _to, T _cost) : to(_to), cost(_cost) {}
};
} // namespace rika

namespace rika {
namespace internal {
template <typename TValue, typename TGraph>
std::pair<TValue, int> visit(int from, int v, const TGraph &g) {
  auto r = std::make_pair(TValue(), v);
  try {
    for (auto it = g[v].begin(); it != g[v].end(); ++it) {
      if (it->to == from)
        continue;
      auto r2 = visit<TValue>(v, it->to, g);
      if (r2.first + it->cost >= r.first)
        r = std::make_pair(r2.first + it->cost, r2.second);
    }
  } catch (bool x) {
    std::cout << "hoge" << std::endl;
  }
  return r;
}
} // namespace internal

template <typename TValue, typename TGraph>
TValue tree_diameter(const TGraph &g) {
  using namespace internal;
  auto r1 = visit<TValue>(-1, 0, g);
  auto r2 = visit<TValue>(-1, r1.second, g);
  return r2.first;
}
} // namespace rika

#include <bits/stdc++.h>
using namespace std;
using namespace rika;
int main() {
  int N;
  cin >> N;
  vector<vector<edge<int>>> g(N * 10);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    g[s].push_back(edge<int>(t, w));
    g[t].push_back(edge<int>(s, w));
  }
  cout << tree_diameter<int>(g) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>

namespace rika {
template <typename T> class edge {
public:
  int to;
  T cost;

  edge() = default;
  edge(const edge<T> &) = default;
  edge(edge<T> &&) = default;
  edge &operator=(const edge<T> &) = default;
  edge &operator=(edge<T> &&) = default;

  ~edge() = default;

  edge(int _to, T _cost) : to(_to), cost(_cost) {}
};
} // namespace rika

namespace rika {
namespace internal {
template <typename TValue, typename TGraph>
std::pair<TValue, int> visit(int from, int v, const TGraph &g) {
  auto r = std::make_pair(TValue(), v);
  for (auto it = g[v].begin(); it != g[v].end(); ++it) {
    if (it->to == from)
      continue;
    auto r2 = visit<TValue>(v, it->to, g);
    if (r2.first + it->cost >= r.first)
      r = std::make_pair(r2.first + it->cost, r2.second);
  }
  return r;
}
} // namespace internal

template <typename TValue, typename TGraph>
TValue tree_diameter(const TGraph &g) {
  using namespace internal;
  auto r1 = visit<TValue>(-1, 0, g);
  auto r2 = visit<TValue>(-1, r1.second, g);
  return r2.first;
}
} // namespace rika

#include <bits/stdc++.h>
using namespace std;
using namespace rika;
int main() {
  int N;
  cin >> N;
  vector<vector<edge<int>>> g(N * 10);
  for (int i = 0; i < N - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    g[s].push_back(edge<int>(t, w));
    g[t].push_back(edge<int>(s, w));
  }
  cout << tree_diameter<int>(g) << endl;
}
Problem p02371 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct edge {
  int src;
  int dst;
  int weight;
};

using graph = vector<vector<edge>>;

pair<int, int> visit(int v, int parent, const graph &G) {
  pair<int, int> r(0, v);
  for (const auto &e : G[v]) {
    if (e.dst == parent) {
      continue;
    }
    auto t = visit(e.dst, v, G);
    t.first += e.weight;
    if (r.first < t.first) {
      r = t;
    }
  }
  return r;
}

int main() {
  int n;
  cin >> n;

  graph G(n);
  for (int i = 0; i < n - 1; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(edge{s, t, w});
    G[t].push_back(edge{t, s, w});
  }
  auto r = visit(0, -1, G);
  auto t = visit(r.second, -1, G);
  cout << t.first << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct edge {
  int src;
  int dst;
  int weight;
};

using graph = vector<vector<edge>>;

pair<int, int> visit(int v, int parent, const graph &G) {
  pair<int, int> r(0, v);
  for (const auto &e : G[v]) {
    if (e.dst == parent) {
      continue;
    }
    auto t = visit(e.dst, v, G);
    t.first += e.weight;
    if (r.first < t.first) {
      r = t;
    }
  }
  return r;
}

int main() {
  int n;
  cin >> n;

  graph G(n);
  for (int i = 0; i < n; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(edge{s, t, w});
    G[t].push_back(edge{t, s, w});
  }
  auto r = visit(0, -1, G);
  auto t = visit(r.second, -1, G);
  cout << t.first << endl;

  return 0;
}
Problem p02371 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
struct edge {
  int to, cost;
};
using P = pair<int, int>;
const int INF = 1 << 29;
const int MAX_V = 100010;
vector<edge> G[MAX_V];
int d[MAX_V];
int V, E;
void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  _;
  int n, f, t, c;
  cin >> n;
  REP(i, n - 1) {
    cin >> f >> t >> c;
    G[f].push_back(edge{t, c});
    G[t].push_back(edge{f, c});
  }
  V = n;
  E = (n - 1) * 2;
  dijkstra(0);
  int maxIdx = distance(d, max_element(d, d + n));
  dijkstra(maxIdx);
  cout << (*max_element(d, d + n)) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
struct edge {
  int from, to, cost;
};

const int INF = 1 << 29;
const int MAX_E = 200010;
const int MAX_V = 100010;
edge es[MAX_E];
int d[MAX_V];
int V, E;
void shortest_path(int s) {
  REP(i, V) d[i] = INF;
  d[s] = 0;
  for (;;) {
    bool update = false;
    REP(i, E) {
      edge e = es[i];
      if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
        d[e.to] = d[e.from] + e.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

int main() {
  _;
  int n, f, t, c;
  cin >> n;
  REP(i, n - 1) {
    cin >> f >> t >> c;
    es[2 * i].from = es[2 * i + 1].to = f;
    es[2 * i].to = es[2 * i + 1].from = t;
    es[2 * i].cost = es[2 * i + 1].cost = c;
  }
  V = n;
  E = (n - 1) * 2;
  shortest_path(0);
  int maxIdx = distance(d, max_element(d, d + n));
  shortest_path(maxIdx);
  cout << (*max_element(d, d + n)) << endl;
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

struct edge {
  int dest, weight, height;
};

int dfs(int prev, int node, vector<vector<edge>> &adjacentList) {
  int res = 0;
  for (edge &e : adjacentList[node]) {
    if (e.dest == prev)
      continue;
    if (e.height == -1)
      e.height = e.weight + dfs(node, e.dest, adjacentList);
    res = max(res, e.height);
  }
  return res;
}

vector<int> heightsOfTree(vector<vector<edge>> &adjacentList) {
  int n = adjacentList.size();
  vector<int> heights(n);
  for (int i = 0; i < n; i++)
    heights[i] = dfs(-1, i, adjacentList);
  return heights;
}

int main() {
  int n;
  cin >> n;
  vector<vector<edge>> adjacentList(n, vector<edge>());
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    adjacentList[s].push_back(edge{t, w, -1});
    adjacentList[t].push_back(edge{s, w, -1});
  }

  vector<int> heights = heightsOfTree(adjacentList);
  for (int i : heights) {
    cout << i << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, N) for (int i = 0; i < (int)N; i++)

struct edge {
  int dest, weight, height;
};

int dfs(int prev, int node, vector<vector<edge>> &adjacentList) {
  int res = 0;
  for (edge e : adjacentList[node]) {
    if (e.dest == prev)
      continue;
    if (e.height == -1)
      e.height = e.weight + dfs(node, e.dest, adjacentList);
    res = max(res, e.height);
  }
  return res;
}

vector<int> heightsOfTree(vector<vector<edge>> &adjacentList) {
  int n = adjacentList.size();
  vector<int> heights(n);
  for (int i = 0; i < n; i++)
    heights[i] = dfs(-1, i, adjacentList);
  return heights;
}

int main() {
  int n;
  cin >> n;
  vector<vector<edge>> adjacentList(n, vector<edge>());
  for (int i = 0; i < n - 1; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    adjacentList[s].push_back(edge{t, w, -1});
    adjacentList[t].push_back(edge{s, w, -1});
  }

  vector<int> heights = heightsOfTree(adjacentList);
  for (int i : heights) {
    cout << i << endl;
  }

  return 0;
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
const int N = 1e4 + 10;
vector<pi> graph[N];
bitset<N> went;
int dep[N][2], top, rad;
int max(int a, int b) { return a > b ? a : b; }
void dfs(int now, bool f) {
  if (dep[now][f] > top) {
    top = dep[now][f];
    rad = now;
  }
  went[now] = true;
  for (pi i : graph[now])
    if (!went[i.F]) {
      dep[i.F][f] = dep[now][f] + i.S;
      dfs(i.F, f);
    }
  return;
}
int main() {
  int n, l, r, w;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    dep[i][0] = dep[i][1] = 0;
  went.reset();
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &l, &r, &w);
    graph[l].PB({r, w});
    graph[r].PB({l, w});
  }
  went.reset();
  top = rad = 0;
  dep[0][false] = 0;
  dfs(0, false);
  went.reset();
  top = 0;
  dep[rad][false] = 0;
  dfs(rad, false);
  went.reset();
  dep[rad][true] = 0;
  dfs(rad, true);
  for (int i = 0; i < n; i++)
    printf("%d\\n", max(dep[i][0], dep[i][1]));
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
int max(int a, int b) {
  if (a > b)
    return a;
  return b;
}
struct node {
  int parent;
  vector<pair<int, int>> child;
};
struct tree {
  node map[100010];
  int find(int now) {
    int ans = 0;
    for (int i = 0; i < map[now].child.size(); i++)
      if (map[now].child[i].first != map[now].parent) {
        map[map[now].child[i].first].parent = now;
        ans =
            max(ans, find(map[now].child[i].first) + map[now].child[i].second);
      }
    return ans;
  }
};
int main() {
  int n, l, r, c;
  tree graph;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d%d", &l, &r, &c);
    graph.map[l].child.push_back(make_pair(r, c));
    graph.map[r].child.push_back(make_pair(l, c));
  }
  for (int i = 0; i < n; i++) {
    graph.map[i].parent = -1;
    printf("%d\\n", graph.find(i));
  }
}
Problem p02372 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

struct edge {
  int first, second, height;
};
int dfs(const int now, const int from, vector<vector<edge>> &es) {

  int len = 0;
  for (auto &e : es[now]) {
    if (e.first == from)
      continue;
    else {
      if (e.height == -1) {
        e.height = dfs(e.first, now, es);
      }
      len = max(len, e.second + e.height);
    }
  }
  return len;
}

int main() {
  int N;
  cin >> N;
  vector<vector<edge>> edges(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    edges[a].push_back(edge{b, c, -1});
    edges[b].push_back(edge{a, c, -1});
  }
  vector<int> hs(N);
  for (int i = 0; i < N; ++i) {
    hs[i] = dfs(i, -1, edges);
    cout << hs[i] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.txt"

vector<vector<pair<int, int>>> v;
int dfs(const int now, const int from,
        const vector<vector<pair<int, int>>> &es) {
  if (v[now].empty()) {
    vector<pair<int, int>> anss(2, make_pair(0, -1));
    for (const auto &e : es[now]) {

      anss.push_back(make_pair(e.second + dfs(e.first, now, es), e.first));
      sort(anss.begin(), anss.end(), greater<pair<int, int>>());
      anss.resize(2);
    }
    v[now] = anss;
  }
  if (v[now][0].second == from) {
    return v[now][1].first;
  } else {
    return v[now][0].first;
  }
}

int main() {
  int N;
  cin >> N;
  vector<vector<pair<int, int>>> edges(N);
  for (int i = 0; i < N - 1; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    edges[a].push_back(make_pair(b, c));
    edges[b].push_back(make_pair(a, c));
  }
  vector<int> hs(N);
  for (int i = 0; i < N; ++i) {
    hs[i] = dfs(i, -1, edges);
    cout << hs[i] << endl;
  }
  return 0;
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)

using namespace std;

struct Edge {
  int node;
  int weight;
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef pair<int, int> PII;

using namespace std;

vector<int> djikstra(Graph g, int v) {
  priority_queue<PII, vector<PII>, greater<PII>> q;

  q.push(PII(0, v));

  vector<int> distances(g.size(), -1);
  vector<bool> seen(g.size(), false);
  distances[v] = 0;
  while (!q.empty()) {
    PII p = q.top();
    q.pop();

    int distance = p.first;
    int node = p.second;
    if (!seen[node]) {
      seen[node] = true;
      for (Edge edge : g[node]) {
        if (distances[edge.node] == -1 ||
            distances[edge.node] > distance + edge.weight) {
          distances[edge.node] = distance + edge.weight;
          q.push(PII(distances[edge.node], edge.node));
        }
      }
    }
  }

  return distances;
}

int getFurtherPoint(Graph g, int v) {
  vector<int> minDistances = djikstra(g, v);

  int m = -1;
  int point = -1;
  FOR(i, minDistances.size()) {
    if (minDistances[i] > m) {
      m = minDistances[i];
      point = i;
    }
  }

  return point;
}

int main() {
  int n;
  cin >> n;

  Graph g(n);

  FOR(_, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    g[s].push_back(Edge{t, w});
    g[t].push_back(Edge{s, w});
  }

  int v1 = getFurtherPoint(g, 0);
  vector<int> d1 = djikstra(g, v1);
  int v2 = getFurtherPoint(g, v1);
  vector<int> d2 = djikstra(g, v2);

  FOR(i, d1.size()) { cout << (d1[i] > d2[i] ? d1[i] : d2[i]) << endl; }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

#define FOR(i, n) for (int i = 0; i < n; i++)

struct Edge {
  int node;
  int weight;
};
struct Ant {
  int node;
  int comingFrom;
  int distance;
};

using namespace std;

int main() {
  int n;
  cin >> n;

  map<int, vector<Edge>> graph;

  FOR(_, n - 1) {
    int s, t, w;
    cin >> s >> t >> w;
    graph[s].push_back(Edge{t, w});
    graph[t].push_back(Edge{s, w});
  }

  // Find leaves in tree
  vector<Ant> ants;
  for (auto connections : graph)
    if (connections.second.size() == 1)
      ants.push_back(Ant{connections.first, -1, 0});

  vector<int> maxDistances(n, 0);
  while (!ants.empty()) {
    vector<Ant> newAnts;
    for (Ant ant : ants) {
      // Find neighbours
      vector<Edge> neighbours = graph[ant.node];
      for (Edge neighbour : neighbours)
        if (neighbour.node != ant.comingFrom) {
          if (ant.distance + neighbour.weight > maxDistances[neighbour.node])
            maxDistances[neighbour.node] = ant.distance + neighbour.weight;
          newAnts.push_back(
              Ant{neighbour.node, ant.node, ant.distance + neighbour.weight});
        }
    }

    ants = newAnts;
  }

  for (auto distance : maxDistances)
    cout << distance << endl;
}
Problem p02372 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for (Int i = 0; i < (Int)(n); ++i)
#define guard(x)                                                               \\
  if (not(x))                                                                  \\
    continue;
#ifndef LOCAL_
#define fprintf                                                                \\
  if (false)                                                                   \\
  fprintf
#endif

int main() {
  Int n;
  std::cin >> n;
  std::vector<Int> ss(n - 1), ts(n - 1), ws(n - 1);
  rep(i, n - 1) std::cin >> ss[i] >> ts[i] >> ws[i];
  std::vector<std::vector<std::pair<Int, Int>>> nexts(n);
  rep(i, n - 1) {
    Int s = ss[i], t = ts[i], w = ws[i];
    nexts[s].emplace_back(t, w);
    nexts[t].emplace_back(s, w);
  }
  std::vector<Int> xs(n, -1);
  std::vector<Int> ys(n, -1);
  Int as[3] = {};
  for (Int t = 0; t < 3; ++t) {
    Int c = as[std::max((Int)0, t - 1)];
    std::vector<Int> &zs = (t % 2 == 0) ? xs : ys;
    zs.clear();
    zs.resize(n, -1);
    std::queue<Int> q;
    zs[c] = 0;
    q.emplace(c);
    while (not q.empty()) {
      Int i = q.front();
      q.pop();
      for (auto kw : nexts[i]) {
        Int k, w;
        std::tie(k, w) = kw;
        guard(zs[k] == -1);
        zs[k] = zs[i] + w;
        q.emplace(k);
      }
    }
    rep(i, n) if (zs[as[t]] < zs[i]) as[t] = i;
  }
  rep(i, n) printf("%ld\\n", std::max(xs[i], ys[i]));
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
#define rep(i, n) for (Int i = 0; i < (Int)(n); ++i)
#define guard(x)                                                               \\
  if (not(x))                                                                  \\
    continue;
#ifndef LOCAL_
#define fprintf                                                                \\
  if (false)                                                                   \\
  fprintf
#endif

int main() {
  Int n;
  std::cin >> n;
  std::vector<Int> ss(n - 1), ts(n - 1), ws(n - 1);
  rep(i, n - 1) std::cin >> ss[i] >> ts[i] >> ws[i];
  std::vector<std::vector<std::pair<Int, Int>>> nexts(n);
  rep(i, n - 1) {
    Int s = ss[i], t = ts[i], w = ws[i];
    nexts[s].emplace_back(t, w);
    nexts[t].emplace_back(s, w);
  }
  std::vector<Int> xs(n, -1);
  std::vector<Int> ys(n, -1);
  Int as[2];
  for (Int t = 0; t < 2; ++t) {
    Int c = t == 0 ? 0 : as[0];
    std::vector<Int> &zs = t == 0 ? xs : ys;
    std::queue<Int> q;
    zs[c] = 0;
    q.emplace(c);
    while (not q.empty()) {
      Int i = q.front();
      q.pop();
      for (auto kw : nexts[i]) {
        Int k, w;
        std::tie(k, w) = kw;
        guard(zs[k] == -1);
        zs[k] = zs[i] + w;
        q.emplace(k);
      }
    }
    rep(i, n) if (zs[as[t]] < zs[i]) as[t] = i;
  }
  rep(i, n) printf("%ld\\n", std::max(xs[i], ys[i]));
}
Problem p02372 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

#define INF 200000000

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

vector<pii> G[10000];
int d1[10000], d2[10000];
pii farestNode;

void visit(int n, int p, int w, int opt) {
  switch (opt) {
  case 1:
    d1[n] = w;
    break;
  case 2:
    d2[n] = w;
    break;
  }
  if (G[n].size() == 1 && G[n][0].first == p) {
    if (w > farestNode.second) {
      farestNode = pii(n, w);
    }
    return;
  }
  for (auto &m : G[n]) {
    if (m.first != p) {
      visit(m.first, n, w + m.second, opt);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  farestNode = pii(-1, 0);

  int V;
  scanf("%d", &V);

  for (int i = 0; i < V - 1; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);

    G[u].push_back(pii(v, w));
    G[v].push_back(pii(u, w));
  }

  visit(0, -1, 0, 0);
  int t = farestNode.first;
  farestNode = pii(-1, 0);
  visit(t, -1, 0, 1);
  t = farestNode.first;
  farestNode = pii(-1, 0);
  visit(t, -1, 0, 2);

  for (int i = 0; i < V; i++) {
    printf("%d\\n", max(d1[i], d2[i]));
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) ((a) > (b) ? (b) : (a))
#define max(a, b) ((a) > (b) ? (a) : (b))

#define INF 200000000

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;

vector<pii> G[100000];
pii farestNode;

void visit(int n, int p, int w) {
  if (G[n].size() == 1 && G[n][0].first == p) {
    if (w > farestNode.second) {
      farestNode = pii(n, w);
    }
    return;
  }
  for (auto &m : G[n]) {
    if (m.first != p) {
      visit(m.first, n, w + m.second);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  farestNode = pii(-1, 0);

  int V;
  scanf("%d", &V);

  for (int i = 0; i < V - 1; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);

    G[u].push_back(pii(v, w));
    G[v].push_back(pii(u, w));
  }

  for (int i = 0; i < V; i++) {
    farestNode = pii(-1, 0);
    visit(i, -1, 0);
    printf("%d\\n", farestNode.second);
  }

  return 0;
}