#include <bits/stdc++.h>
#define fir first
#define sec second
#define ll long long
using namespace std;
const int mxN=200020;
const ll INF=1000000000000000001;
int N, Q;
map <int, ll> v[mxN];
ll ans[mxN];
ll sum;
ll tot;
void dfs(int now, int pre)
{
for(auto iter=v[now].begin();iter!=v[now].end();iter++)
{
if(iter->fir==pre) continue;
sum+=iter->sec;
dfs(iter->fir, now);
}
}
void solv1()
{
ll preans=0;
for(int i=1;i<=N;i++)
{
sum=0;
dfs(i, -1);
preans=max(preans, sum);
}
ans[1]=tot-preans;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
for(int i=0;i<N-1;i++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
v[a].insert({b, d});
v[b].insert({a, c});
tot+=c; tot+=d;
}
solv1();
cin >> Q;
while(Q--)
{
int a;
cin >> a;
cout << ans[a] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Execution timed out |
2098 ms |
35820 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Execution timed out |
2099 ms |
41196 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Execution timed out |
2098 ms |
35820 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Incorrect |
6 ms |
9708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |