structEdge { int u, v; }; std::vector<Edge> G[maxn]; int faa[maxn]; int rem[maxn][2];
intdfs(int cur, int t){ if (rem[cur][t] != 0) { return rem[cur][t]; } if (G[cur].size() == 0) { if (t == 0) return0; elsereturn rem[cur][t] = r[cur]; } int ans = 0; if (t == 0) { for (Edge e : G[cur]) { ans += std::max(dfs(e.v, 0), dfs(e.v, 1)); } } else { for (Edge e : G[cur]) { ans += dfs(e.v, 0); } ans += r[cur]; } return rem[cur][t] = ans;
}
intmain(){ #ifdef LOCAL freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif int n; read(n); for (int i = 1; i <= n; i++) { read(r[i]); }
int u, v; for (int i = 1; i <= n - 1; i++) { read(u, v); G[v].push_back({v, u}); faa[u]++; } int fa; for (int i = 1; i <= n; i++) { if (!faa[i]) { fa = i; break; } } writeln(std::max(dfs(fa, 1), dfs(fa, 0))); return0; }
int n, m; constint maxn = 400005; structEdge { int u, v, w; }; std::vector<Edge> G[maxn]; int in[maxn], out[maxn]; int L[maxn], cnt = 0; std::vector<Edge> P[maxn]; double p[maxn], f[maxn];
voidtopo(){ std::queue<int> q; for (int i = 1; i <= n; i++) { if (!in[i]) q.push(i); } while (!q.empty()) { int ft = q.front(); q.pop(); L[++cnt] = ft; for (auto e : G[ft]) { if (--in[e.v] == 0) { q.push(e.v); } } } }
intmain(){ #ifdef LOCAL freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif read(n, m); for (int i = 1; i <= m; i++) { staticint u, v, w; read(u, v, w); G[u].push_back({u, v, w}); P[v].push_back({v, u, w}); in[v]++; out[u]++; } topo();
p[1] = 1; for (int i = 2; i <= cnt; i++) { for (auto e : P[L[i]]) { p[e.u] += 1.0 / out[e.v] * p[e.v]; f[e.u] += 1.0 / out[e.v] * p[e.v] * e.w; } // printf("%d %.2lf %.2lf\n", L[i], p[L[i]], f[L[i]]); } double ans = 0; for (int i = 1; i <= n; i++) ans += f[i]; printf("%.2lf\n", ans); return0; }
int f[maxn], in[maxn]; int cnt1[maxn]; // bool vis1[maxn]; // void dfs1(int u, int id) { // if (vis1[u]) return; // vis1[u] = true; // for (auto e : G[u]) { // if (pc[e.v] == id) { // in[id]--; // dfs1(e.v, id); // while (e.w > 0) { // cnt1[id] += e.w; // e.w = e.w * e.q; // } // } // } // }
std::vector<Edge> G2[maxn];
voidbfs2(int s){ f[s] = cnt1[s]; // writeln(f[s]); std::queue<int> q; // q.push(s); for (int i = 1; i <= pot.size(); i++) { if (in[i] == 0) q.push(i); } while (!q.empty()) { int ft = q.front(); q.pop(); // ft is ok in default for (auto e : G2[ft]) { f[e.v] = std::max(f[e.v], f[ft] + e.w + cnt1[e.v]); in[e.v]--; if (in[e.v] == 0) q.push(e.v); } } }
intmain(){ #ifdef LOCAL freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif int s; memset(pc, -1, sizeof(pc)); memset(f, -63, sizeof(f)); read(n, m); for (int i = 1; i <= m; i++) { staticint u, v, w; staticdouble q; read(u, v, w); scanf("%lf", &q); G[u].push_back({u, v, w, int(q * 10 + 0.0001), false}); // in[v]++; } read(s);
for (int i = 1; i <= n; i++) if (!vis[i]) tarjan(i); // for (auto v : pot) { // if (v.size() == 1) { // pc[v[0]] = v[0]; // continue; // } // int id = ++n; // pc[id] = id; // for (int q : v) { // pc[q] = id; // in[id] += in[q]; // } // dfs1(v[0], id); // for (int q : v) { // for (auto e : G[q]) { // if (pc[e.v] == id) continue; // e.u = id; // G[id].push_back(e); // } // } // } for (int i = 1; i <= n; i++) { for (auto e : G[i]) { if (pc[e.u] != pc[e.v]) { G2[pc[e.u]].push_back({pc[e.u], pc[e.v], e.w, e.q, e.re}); in[pc[e.v]]++; } else { while (e.w > 0) { cnt1[pc[e.u]] += e.w; e.w = e.w * e.q; e.w /= 10; } } } }
// writeln(pc[s], in[pc[s]]); bfs2(pc[s]); // writeln(pot.size()); int ans = 0; for (int i = 1; i <= pot.size(); i++) { ans = std::max(ans, f[i]); // writeln(f[i]); } writeln(ans); return0; }
int Nw[maxn], Nv[maxn]; bool vis[maxn][maxn]; int in[maxn]; std::vector<Edge> Gn[maxn];
std::queue<int> q; voidsort(){ memset(f, -63, sizeof(f)); for (int i = 1; i <= ct; i++) { if (!in[i]) q.push(i); f[i][0] = 0; } while (!q.empty()) { int u = q.front(); q.pop(); for (int j = m; j >= Nv[u]; j--) { f[u][j] = f[u][j - Nv[u]] + Nw[u]; } for (int j = Nv[u] - 1; j >= 1; j--) { f[u][j] = -998244353; } for (auto e : Gn[u]) { for (int j = m; j >= 0; j--) { for (int k = 0; k <= j; k++) { f[e.v][j] = std::max(f[e.v][j], f[e.v][j - k] + f[u][k]); } } in[e.v]--; if (!in[e.v]) { q.push(e.v); } } } }
intmain(){ #ifdef LOCAL freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif read(n, m); for (int i = 1; i <= n; i++) read(v[i]); for (int i = 1; i <= n; i++) read(w[i]); for (int i = 1; i <= n; i++) { read(d[i]); insert(i, d[i]); }
for (int i = 0; i <= n; i++) if (!dfn[i]) tarjan(i);
for (int i = 0; i <= n; i++) { Nw[id[i]] += w[i]; Nv[id[i]] += v[i]; bool flag = 0; for (auto e : G[i]) { if (id[i] != id[e.v] && !vis[id[i]][id[e.v]]) { Gn[id[i]].push_back({id[i], id[e.v]}); vis[id[i]][id[e.v]] = true; in[id[e.v]]++; flag |= true; } } if (!flag && !vis[id[i]][id[0]]){ Gn[id[i]].push_back({id[i], id[0]}); vis[id[i]][id[0]] = true; in[id[0]]++; } } sort(); int ans = 0; for (int q = 0; q <= 0; q++) for (int i = Nv[id[q]]; i <= m; i++) { ans = std::max(ans, f[id[q]][i]); } writeln(ans); return0; }
structEdge { int u, v, w; }; std::vector<Edge> G[maxn];
voidinsert(int u, int v, int w){ G[u].push_back({u, v, w}); G[v].push_back({v, u, w}); }
int f[maxn]; int sum[maxn]; voiddfs1(int u, int fa, int w){ sum[u] = c[u]; f[1] += w * c[u]; for (auto e : G[u]) { if (e.v != fa) { dfs1(e.v, u, w + e.w); sum[u] += sum[e.v]; } } }
voiddfs2(int u, int fa){ for (auto e : G[u]) { if (e.v != fa) { f[e.v] = f[u] + e.w * (sum[1] - sum[e.v] * 2); dfs2(e.v, u); } } }
signedmain(){ #ifdef LOCAL freopen(".in", "r", stdin); freopen(".out", "w", stdout); #endif int n; read(n); for (int i = 1; i <= n; i++) { read(c[i]); } for (int i = 1; i < n; i++) { staticint a, b, l; read(a, b, l); insert(a, b, l); }
dfs1(1, -1, 0); dfs2(1, -1); int ans = f[1]; for (int i = 2; i <= n; i++) { ans = std::min(ans, f[i]); } writeln(ans); return0; }