#include <bits/stdc++.h>
#define maxn 200005
#define INF (long long)1e18
#define pii pair<int,int>
#define sus first
#define w second
using namespace std;
/*** 20:50***/
int n;
vector<pii>adj[maxn];
long long dp[maxn];
long long down[maxn],up[maxn];
void dfs(int v,int p)
{
down[v]=0LL;
for(auto x:adj[v])
{
if(x.sus!=p)
{
down[v]+=x.w;
dfs(x.sus,v);
down[v]+=down[x.sus];
}
}
}
void dfs2(int v,int p,int tez)
{
up[v]=0LL;
for(auto x:adj[v])
{
if(x.sus==p)
{
up[v]+=up[p];
up[v]+=x.w;
up[v]+=down[p]-tez-down[v];
}
}
for(auto x:adj[v])
{
if(x.sus!=p)dfs2(x.sus,v,x.w);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
long long SUM=0LL;
for(int i=1;i<n;i++)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
adj[a].push_back({b,c});
adj[b].push_back({a,d});
SUM+=c+d;
}
dfs(1,1);
dfs2(1,1,0);
dp[1]=INF;
for(int i=1;i<=n;i++)
{
dp[1]=min(dp[1],up[i]+down[i]);
}
//for(int i=1;i<=n;i++)cout<<i<<": "<<up[i]<<" "<<down[i]<<endl;
int q;
cin>>q;
while(q>0)
{
int e;
cin>>e;
cout<<dp[e]<<endl;
q--;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5184 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
249 ms |
17500 KB |
Output is correct |
3 |
Correct |
292 ms |
30968 KB |
Output is correct |
4 |
Correct |
236 ms |
20512 KB |
Output is correct |
5 |
Correct |
235 ms |
21896 KB |
Output is correct |
6 |
Correct |
271 ms |
23288 KB |
Output is correct |
7 |
Correct |
207 ms |
22056 KB |
Output is correct |
8 |
Correct |
285 ms |
31480 KB |
Output is correct |
9 |
Correct |
171 ms |
22620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Incorrect |
301 ms |
21896 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5184 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
249 ms |
17500 KB |
Output is correct |
3 |
Correct |
292 ms |
30968 KB |
Output is correct |
4 |
Correct |
236 ms |
20512 KB |
Output is correct |
5 |
Correct |
235 ms |
21896 KB |
Output is correct |
6 |
Correct |
271 ms |
23288 KB |
Output is correct |
7 |
Correct |
207 ms |
22056 KB |
Output is correct |
8 |
Correct |
285 ms |
31480 KB |
Output is correct |
9 |
Correct |
171 ms |
22620 KB |
Output is correct |
10 |
Correct |
6 ms |
4984 KB |
Output is correct |
11 |
Incorrect |
301 ms |
21896 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Incorrect |
6 ms |
5184 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |