#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];
ll ans[mxN];
ll tot, rtot;
ll dep[mxN], rdep[mxN];
void dfs(int now, int pre)
{
for(auto iter=v[now].begin();iter!=v[now].end();iter++)
{
if(iter->fir==pre) continue;
int nxt=iter->fir;
dep[nxt]=dep[now]+iter->sec.sec;
tot+=iter->sec.sec;
dfs(nxt, now);
}
}
void rdfs(int now, int pre)
{
for(pll ele : rv[now])
{
if(ele.fir==pre) continue;
rdep[ele.fir]=rdep[now]+ele.sec;
rdfs(ele.fir, now);
}
}
void solv1()
{
dfs(1, -1);
rdfs(1, -1);
for(int i=1;i<=N;i++)
{
ans[1]=max(ans[1], tot-dep[i]+rdep[i]);
}
ans[1]=rtot-ans[1];
}
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});
rtot+=c; rtot+=d;
}
solv1();
cin >> Q;
while(Q--)
{
int a;
cin >> a;
cout << ans[a] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Incorrect |
10 ms |
14444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14592 KB |
Output is correct |
2 |
Correct |
458 ms |
52768 KB |
Output is correct |
3 |
Correct |
513 ms |
63756 KB |
Output is correct |
4 |
Correct |
449 ms |
53432 KB |
Output is correct |
5 |
Correct |
487 ms |
53460 KB |
Output is correct |
6 |
Correct |
472 ms |
56172 KB |
Output is correct |
7 |
Correct |
482 ms |
53852 KB |
Output is correct |
8 |
Correct |
505 ms |
65388 KB |
Output is correct |
9 |
Correct |
533 ms |
53844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Incorrect |
476 ms |
52844 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Incorrect |
10 ms |
14444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14592 KB |
Output is correct |
2 |
Correct |
458 ms |
52768 KB |
Output is correct |
3 |
Correct |
513 ms |
63756 KB |
Output is correct |
4 |
Correct |
449 ms |
53432 KB |
Output is correct |
5 |
Correct |
487 ms |
53460 KB |
Output is correct |
6 |
Correct |
472 ms |
56172 KB |
Output is correct |
7 |
Correct |
482 ms |
53852 KB |
Output is correct |
8 |
Correct |
505 ms |
65388 KB |
Output is correct |
9 |
Correct |
533 ms |
53844 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Incorrect |
476 ms |
52844 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14444 KB |
Output is correct |
2 |
Incorrect |
10 ms |
14444 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |