#include <bits/stdc++.h>
#define fir first
#define sec second
#define ll long long
#define pll pair<ll, ll>
using namespace std;
const int mxN=200020;
const ll INF=1000000000000000001;
typedef struct line{
int s, e;
ll val;
}line;
int N, Q;
map <int, pair<int, ll>> v[mxN];
vector <pll> rv[mxN];
vector <line> E;
ll ans[mxN];
ll tot;
ll sub[mxN], rsub[mxN];
void dfs(int now, int pre)
{
for(auto iter=v[now].begin();iter!=v[now].end();iter++)
{
if(iter->fir==pre) continue;
dfs(iter->sec.fir, now);
sub[now]+=sub[iter->fir];
rsub[now]+=rsub[iter->fir];
sub[now]+=iter->sec.sec;
}
for(pll ele : rv[now])
{
if(ele.fir!=pre) rsub[now]+=ele.sec;
if(ele.fir!=pre) tot+=ele.sec;
}
}
void solv1()
{
ans[1]=INF;
dfs(1, -1);
for(int i=1;i<=N;i++)
{
ans[1]=min(ans[1], tot-rsub[i]+sub[i]);
}
}
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, {2*i, d}});
v[b].insert({a, {2*i+1, c}});
rv[a].push_back({b, c});
rv[b].push_back({a, d});
}
solv1();
cin >> Q;
while(Q--)
{
int a;
cin >> a;
cout << ans[a] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14444 KB |
Output is correct |
2 |
Incorrect |
9 ms |
14444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Runtime error |
351 ms |
100512 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
333 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14444 KB |
Output is correct |
2 |
Incorrect |
9 ms |
14444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Runtime error |
351 ms |
100512 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14444 KB |
Output is correct |
2 |
Incorrect |
9 ms |
14444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |