1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| #include <bits/stdc++.h> using namespace std;
namespace TYC { typedef long long ll; const int N = 366666 * 2 + 5, M = 20; const ll INF = 0x3f3f3f3f3f3f3f3fLL;
int n, node_cnt, rot[N]; ll ans; struct edge { int v, w; }; struct node { int son[2]; ll val; } tr[N * M];
namespace T1 { int tot, size[N], Head[N], now[N]; ll dis[N]; vector<edge> T[N]; struct Edge { int to, next, w, vis; } E[N << 1];
inline void add(const int u, const int v, const int w) { static int ec = 1; E[++ec] = {v, Head[u], w, 0}; Head[u] = ec; E[++ec] = {u, Head[v], w, 0}; Head[v] = ec; }
void buildtree(const int u, const int f) { for (auto p : T[u]) if (p.v != f) { dis[p.v] = dis[u] + p.w; buildtree(p.v, u); } static vector<edge> son; son.clear(); for (auto p : T[u]) if (p.v != f) son.push_back(p); int sz = int(son.size()); if (!sz) return; for (int i = 0; i < sz; i++) add(tot + i + 1, son[i].v, son[i].w); for (int i = 1; i < sz; i++) add(tot + i, tot + i + 1, 0); add(u, tot + 1, 0); tot += sz; }
void build() { tot = n; for (int i = 1, u, v, w; i < n; i++) { scanf("%d%d%d", &u, &v, &w); T[u].push_back({v, w}); T[v].push_back({u, w}); } buildtree(1, 0); for (int i = 1; i <= n; i++) rot[i] = now[i] = ++node_cnt; }
void getsize(const int u, const int f) { size[u] = 1; for (int i = Head[u], v; i; i = E[i].next) if (!E[i].vis && (v = E[i].to) != f) { getsize(v, u); size[u] += size[v]; } }
void getedge(const int u, const int f, const int sz, int &p) { for (int i = Head[u], v; i; i = E[i].next) if (!E[i].vis && (v = E[i].to) != f) { if (max(size[v], sz - size[v]) < max(size[E[p].to], sz - size[E[p].to])) p = i; getedge(v, u, sz, p); } }
void dfs(const int u, const int f, const ll d, const int c) { if (u <= n) { tr[now[u]].son[c] = ++node_cnt; tr[now[u] = node_cnt].val = d + dis[u]; } for (int i = Head[u], v; i; i = E[i].next) if (!E[i].vis && (v = E[i].to) != f) dfs(v, u, d + E[i].w, c); }
void solve(const int x) { getsize(x, 0); if (size[x] == 1) return; int p = 0, u, v; getedge(x, 0, size[x], p); E[p].vis = E[p ^ 1].vis = 1; dfs(u = E[p].to, 0, E[p].w, 0); dfs(v = E[p ^ 1].to, 0, 0, 1); solve(u); solve(v); } }
namespace T2 { ll res; vector<edge> E[N];
void update(const int x, const int y) { if (x && y) ans = max(ans, tr[x].val + tr[y].val - res); }
void merge(int &x, const int y) { if (!x || !y) return void(x = x + y); tr[x].val = max(tr[x].val, tr[y].val); update(tr[x].son[0], tr[y].son[1]); update(tr[x].son[1], tr[y].son[0]); merge(tr[x].son[0], tr[y].son[0]); merge(tr[x].son[1], tr[y].son[1]); } void dfs(const int u, const int f, const ll d) { ans = max(ans, (T1::dis[u] - d) * 2); for (auto p : E[u]) if (p.v != f) { dfs(p.v, u, d + p.w); res = d * 2; merge(rot[u], rot[p.v]); } }
void solve() { for (int i = 1, u, v, w; i < n; i++) { scanf("%d%d%d", &u, &v, &w); E[u].push_back({v, w}); E[v].push_back({u, w}); } dfs(1, 0, 0); } }
void work() { scanf("%d", &n); T1::build(); T1::solve(1); T2::solve(); printf("%lld\n", ans >> 1); } }
int main() { TYC::work(); return 0; }
|