#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 2e5+10;
int n, q, up[maxn], dw[maxn], h[maxn];
vector<int> g[maxn];
vector<pair<pair<int,int>,pair<int,int>>> edg;
void dfsh(int u, int ant) {
for(auto id : g[u]) {
int v = edg[id].fr.fr^edg[id].fr.sc^u;
if(v == ant) continue;
if(v == edg[id].fr.fr) {
swap(edg[id].fr.fr,edg[id].fr.sc);
swap(edg[id].sc.fr,edg[id].sc.sc);
//edg[i].sc.fr -> descendo
//edg[i].sc.sc -> subindo
}
h[v] = h[u]+1;
dfsh(v,u);
}
}
void dfsdu(int u, int ant) {
for(auto id : g[u]) {
int v = edg[id].fr.fr^edg[id].fr.sc^u;
if(v == ant) continue;
dw[v] = dw[u]+edg[id].sc.fr;
up[v] = up[u]+edg[id].sc.sc;
dfsdu(v,u);
}
}
void solve() {
cin >> n;
int ans = 0;
for(int i = 0; i < n-1; i++) {
int u,v,w1,w2;
cin >> u >> v >> w1 >> w2;
g[u].pb(i);
g[v].pb(i);
ans+= w1+w2;
edg.pb(mp(mp(u,v),mp(w1,w2)));
}
dfsh(1,0);
int tup = 0;
for(int i = 0; i < n-1; i++) {
tup+= edg[i].sc.sc;
}
dfsdu(1,0);
cin >> q;
int ans1 = 0;
for(int i = 1; i <= n; i++) {
ans1 = max(ans1, tup+dw[i]-up[i]);
}
cout << ans-ans1 << endl;
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
230 ms |
29732 KB |
Output is correct |
3 |
Correct |
272 ms |
41264 KB |
Output is correct |
4 |
Correct |
237 ms |
28224 KB |
Output is correct |
5 |
Correct |
213 ms |
29648 KB |
Output is correct |
6 |
Correct |
241 ms |
31368 KB |
Output is correct |
7 |
Correct |
175 ms |
29732 KB |
Output is correct |
8 |
Correct |
256 ms |
41816 KB |
Output is correct |
9 |
Correct |
139 ms |
30296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
230 ms |
29732 KB |
Output is correct |
3 |
Correct |
272 ms |
41264 KB |
Output is correct |
4 |
Correct |
237 ms |
28224 KB |
Output is correct |
5 |
Correct |
213 ms |
29648 KB |
Output is correct |
6 |
Correct |
241 ms |
31368 KB |
Output is correct |
7 |
Correct |
175 ms |
29732 KB |
Output is correct |
8 |
Correct |
256 ms |
41816 KB |
Output is correct |
9 |
Correct |
139 ms |
30296 KB |
Output is correct |
10 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |