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 <bits/stdc++.h>
using namespace std;
struct edge {
int u, v, c, d;
int other( int p ) {
return u ^ v ^ p;
}
int cost( int p ) {
if ( p == u )
return d;
return c;
}
};
const int MAX_N = 2e5;
int parent[MAX_N + 1];
long long cost[MAX_N + 1], parentEdgeCost[MAX_N + 1];
long long sumCost;
edge edges[MAX_N];
vector<int> adj[MAX_N + 1];
void dfs( int u, int p ) {
for ( int e: adj[u] ) {
int v = edges[e].other( u ), c = edges[e].cost( u ), d = (edges[e].c ^ edges[e].d ^ c);
if ( v == p )
continue;
parent[v] = u;
cost[v] = cost[u] + d;
sumCost += c;
dfs( v, u );
}
}
int main() {
int n;
long long sumTotal = 0;
cin >> n;
for ( int i = 0; i < n - 1; i++ ) {
int u, v, c, d;
cin >> u >> v >> c >> d;
edges[i] = { u, v, c, d };
adj[u].push_back( i );
adj[v].push_back( i );
sumTotal += c + d;
}
int q, e;
cin >> q >> e;
sumCost = 0;
dfs( 1, 0 );
int r = 1;
for ( int v = 2; v <= n; v++ ) {
if ( cost[v] > cost[r] )
r = v;
}
sumCost = 0;
cost[r] = 0;
dfs( r, 0 );
long long maxx = 0;
for ( int v = 1; v <= n; v++ ) {
if ( cost[v] > maxx )
maxx = cost[v];
}
cout << sumTotal - (sumCost + maxx);
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... |