#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2e5 + 10;
struct edge
{
int v, u;
ll c, d;
edge(int _v = 0, int _u = 0, ll _c = 0, ll _d = 0)
{
v = _v;
u = _u;
c = _c;
d = _d;
}
}edges[maxn];
int n;
vector < pair < int, int > > adj[maxn];
ll sum_all = 0;
void input()
{
cin >> n;
for (int i = 1; i < n; i ++)
{
cin >> edges[i].v >> edges[i].u >> edges[i].c >> edges[i].d;
adj[edges[i].v].push_back({edges[i].u, i});
adj[edges[i].u].push_back({edges[i].v, i});
sum_all += edges[i].c + edges[i].d;
}
}
const ll inf = 1e18;
ll single = 0;
ll sub[maxn];
void calc(int v, int p)
{
for (pair < int, int > nb : adj[v])
{
int u = nb.first;
if (u == p)
continue;
calc(u, v);
if (v == edges[nb.second].v)
sub[v] += edges[nb.second].d; //cout << "edge " << v << " : " << u << " : " << edges[nb.second].d << endl;
else
sub[v] += edges[nb.second].c;
sub[v] += sub[u];
}
//cout << "calc " << v << " " << sub[v] << endl;
}
void single_dfs(int v, int p, ll val)
{
single = max(single, val);
///cout << v << " : " << val << endl;
for (pair < int, int > nb : adj[v])
{
int u = nb.first;
if (u == p)
continue;
ll cur;
if (v == edges[nb.second].v)
cur = edges[nb.second].c - edges[nb.second].d;
else
cur = edges[nb.second].d - edges[nb.second].c;
single_dfs(u, v, val + cur);
}
}
void solve_single()
{
calc(1, -1);
single_dfs(1, -1, sub[1]);
int q;
cin >> q;
int e;
cin >> e;
cout << sum_all - single << endl;
}
void solve()
{
input();
solve_single();
}
int main()
{
speed();
int t = 1;
///cin >> t;
while(t --)
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
149 ms |
25112 KB |
Output is correct |
3 |
Correct |
243 ms |
36796 KB |
Output is correct |
4 |
Correct |
145 ms |
23732 KB |
Output is correct |
5 |
Correct |
124 ms |
24892 KB |
Output is correct |
6 |
Correct |
205 ms |
27000 KB |
Output is correct |
7 |
Correct |
125 ms |
25028 KB |
Output is correct |
8 |
Correct |
163 ms |
37148 KB |
Output is correct |
9 |
Correct |
83 ms |
24140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9820 KB |
Output is correct |
2 |
Correct |
149 ms |
25112 KB |
Output is correct |
3 |
Correct |
243 ms |
36796 KB |
Output is correct |
4 |
Correct |
145 ms |
23732 KB |
Output is correct |
5 |
Correct |
124 ms |
24892 KB |
Output is correct |
6 |
Correct |
205 ms |
27000 KB |
Output is correct |
7 |
Correct |
125 ms |
25028 KB |
Output is correct |
8 |
Correct |
163 ms |
37148 KB |
Output is correct |
9 |
Correct |
83 ms |
24140 KB |
Output is correct |
10 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
9820 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |