This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long
#define int long long
// #define int unsigned long long
const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;
pii rts;
vector<pair<int, pii>> G[200005];
int n, q, val[200005], ans[200005], dep[200005], dp[200005], mx;
bool comp(int a, int b) {
return (val[a] + dep[a] > val[b] + dep[b]);
}
void temp_dfs(int v, int par) {
for (auto u : G[v]) {
if (u.ff == par) continue;
val[1] += u.sc.sc;
dep[u.ff] = dep[v] + u.sc.ff;
temp_dfs(u.ff, v);
}
}
void reroot_dfs(int v, int par) {
for (auto u : G[v]) {
if (u.ff == par) continue;
val[u.ff] = val[v] - u.sc.sc + u.sc.ff;
reroot_dfs(u.ff, v);
}
}
void pair_dfs(int v, int par) {
vector<int> vec = {v};
for (auto u : G[v]) {
if (u.ff == par) continue;
pair_dfs(u.ff, v); vec.pb({dp[u.ff]});
} sort(vec.begin(), vec.end(), comp);
dp[v] = vec[0];
if (vec.size() == 1) return;
int cur = val[vec[0]] + val[vec[1]] + dep[vec[0]] + dep[vec[1]] - 2 * dep[v];
if (cur > mx) {
mx = cur; rts = {vec[0], vec[1]};
}
}
void solve() {
cin >> n;
int sum = 0;
for (int i = 1; i < n; i++) {
int a, b, x, y;
cin >> a >> b >> x >> y;
G[a].pb({b, {x, y}});
G[b].pb({a, {y, x}});
sum += x + y;
} temp_dfs(1, 0); reroot_dfs(1, 0); pair_dfs(1, 0);
mx /= 2;
cout << sum - mx;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |