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 ) {
//printf( "%d\n", u );
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;
//printf( "%d %d %d %d\n", u, v, c, d );
parent[v] = u;
cost[v] = cost[u] - c + 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 );
long long maxx = 0;
for ( int v = 1; v <= n; v++ )
maxx = max( maxx, cost[v] );
cout << sumTotal - (sumCost + maxx);
/*for ( int r = 1; r <= n; r++ ) {
sumCost = sumTotal;
dfs( r, 0 );
for ( int i = 2; i <= n; i++ ) {
}
}*/
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... |