#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], ans[maxn], tin[maxn], tout[maxn], timer;
pair<int,int> pai[maxn];
vector<int> g[maxn];
vector<pair<pair<int,int>,pair<int,int>>> edg;
void dfsh(int u, int ant) {
tin[u] = ++timer;
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;
pai[v] = mp(u,id);
dfsh(v,u);
}
tout[u] = timer;
}
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;
ans[0] = 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[0]+= w1+w2;
edg.pb(mp(mp(u,v),mp(w1,w2)));
}
dfsh(1,0);
for(int j = 1; j <= 1; j++) {
pair<int,int> ans1 = mp(-inf,0);
dfsdu(1,0);
int tup = 0;
for(int i = 0; i < n-1; i++) {
tup+= edg[i].sc.sc;
}
for(int i = 1; i <= n; i++) {
ans1 = max(ans1,mp(tup+dw[i]-up[i],i));
}
ans[j] = ans[j-1]-ans1.fr;
int v = ans1.sc;
// tira as arestas pegadas
for(int i = 2; i <= n; i++) {
//muda a aresta de i para pai[i].fr
int id = pai[i].sc;
if(tin[v] >= tin[i] && tout[v] <= tout[i]) {
//se v é filho de i -> tira a aresta descendo
edg[id].sc.fr = 0;
}
else {
edg[id].sc.sc = 0;
}
}
}
cin >> q;
cout << ans[1] << 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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
253 ms |
29636 KB |
Output is correct |
3 |
Correct |
316 ms |
41128 KB |
Output is correct |
4 |
Correct |
215 ms |
29552 KB |
Output is correct |
5 |
Correct |
231 ms |
29612 KB |
Output is correct |
6 |
Correct |
237 ms |
31248 KB |
Output is correct |
7 |
Correct |
194 ms |
29600 KB |
Output is correct |
8 |
Correct |
296 ms |
41676 KB |
Output is correct |
9 |
Correct |
155 ms |
29992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
253 ms |
29636 KB |
Output is correct |
3 |
Correct |
316 ms |
41128 KB |
Output is correct |
4 |
Correct |
215 ms |
29552 KB |
Output is correct |
5 |
Correct |
231 ms |
29612 KB |
Output is correct |
6 |
Correct |
237 ms |
31248 KB |
Output is correct |
7 |
Correct |
194 ms |
29600 KB |
Output is correct |
8 |
Correct |
296 ms |
41676 KB |
Output is correct |
9 |
Correct |
155 ms |
29992 KB |
Output is correct |
10 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |