#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 <line> E;
ll ans[mxN];
ll tot;
bool Chk[2*mxN];
ll eval[2*mxN];
void solv_edge(int num)
{
Chk[num]=true;
eval[num]=E[num].val;
int now=E[num].e;
for(auto iter=v[now].begin();iter!=v[now].end();iter++)
{
if(iter->fir==E[num].s) continue;
if(Chk[iter->sec.fir]) eval[num]+=eval[iter->sec.fir];
else
{
solv_edge(iter->sec.fir);
eval[num]+=eval[iter->sec.fir];
}
}
}
void solv1()
{
ans[1]=INF;
for(int i=0;i<2*N-2;i++)
{
if(Chk[i]) continue;
solv_edge(i);
}
for(int i=1;i<=N;i++)
{
ll sum=0;
for(auto iter=v[i].begin();iter!=v[i].end();iter++)
{
sum+=eval[iter->sec.fir];
}
ans[1]=min(ans[1], tot-sum);
}
}
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}});
E.push_back({a, b, d});
E.push_back({b, 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 |
Correct |
455 ms |
44616 KB |
Output is correct |
3 |
Correct |
477 ms |
53832 KB |
Output is correct |
4 |
Correct |
456 ms |
45548 KB |
Output is correct |
5 |
Execution timed out |
2072 ms |
45512 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9708 KB |
Output is correct |
2 |
Incorrect |
453 ms |
44616 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 |
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 |
Correct |
455 ms |
44616 KB |
Output is correct |
3 |
Correct |
477 ms |
53832 KB |
Output is correct |
4 |
Correct |
456 ms |
45548 KB |
Output is correct |
5 |
Execution timed out |
2072 ms |
45512 KB |
Time limit exceeded |
6 |
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 |
- |