#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;
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;
dep[u.ff] = dep[v] + u.sc.ff + u.sc.sc;
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);
cout << sum - mx / 2;
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Correct |
129 ms |
30776 KB |
Output is correct |
3 |
Correct |
176 ms |
59840 KB |
Output is correct |
4 |
Correct |
116 ms |
29520 KB |
Output is correct |
5 |
Correct |
114 ms |
31232 KB |
Output is correct |
6 |
Correct |
136 ms |
34900 KB |
Output is correct |
7 |
Correct |
112 ms |
32724 KB |
Output is correct |
8 |
Correct |
173 ms |
46672 KB |
Output is correct |
9 |
Correct |
105 ms |
32736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |