#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,q;
struct edge
{
ll x,value_x,value_y;
edge(ll _x=0,ll _value_x=0,ll _value_y=0)
{
x=_x,value_x=_value_x,value_y=_value_y;
}
};
vector<vector<edge> >a;
struct SUB1
{
ll n;
vector<ll>num_child,num_par;
SUB1(ll _n=0)
{
n=_n;
num_child.resize(n+7);
num_par.resize(n+7);
}
void dfs_down(ll x,ll par)
{
for(edge need:a[x])
{
ll node=need.x;
ll value_x=need.value_x;
ll value_y=need.value_y;
if(node==par)continue;
dfs_down(node,x);
num_child[x]+=num_child[node]+value_x;
}
}
void dfs_up(ll x,ll par)
{
for(edge need:a[x])
{
ll node=need.x;
ll value_x=need.value_x;
ll value_y=need.value_y;
if(node==par)continue;
ll value=num_child[x]-num_child[node];
value+=num_par[x]+value_y-value_x;
num_par[node]=value;
dfs_up(node,x);
}
}
void calc()
{
dfs_down(1,0);
dfs_up(1,0);
ll ans=num_child[1];
for(ll i=2;i<=n;i++)ans=min(ans,num_par[i]+num_child[i]);
//for(ll i=1;i<=n;i++)cout<<num_par[i]<<" "<<num_child[i]<<endl;
cout<<ans;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n;
cin>>n;
a.resize(n+7);
for(ll i=1;i<=n-1;i++)
{
ll x,y,value_x,value_y;
cin>>x>>y>>value_x>>value_y;
a[x].push_back({y,value_x,value_y});
a[y].push_back({x,value_y,value_x});
}
ll q;
cin>>q;
if(q==1)
{
ll x;
cin>>x;
if(x==1)
{
SUB1 s(n);
s.calc();
}
}
}
Compilation message
designated_cities.cpp: In member function 'void SUB1::dfs_down(long long int, long long int)':
designated_cities.cpp:30:16: warning: unused variable 'value_y' [-Wunused-variable]
30 | ll value_y=need.value_y;
| ^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
141 ms |
21216 KB |
Output is correct |
3 |
Correct |
185 ms |
34724 KB |
Output is correct |
4 |
Correct |
157 ms |
26236 KB |
Output is correct |
5 |
Correct |
150 ms |
27316 KB |
Output is correct |
6 |
Correct |
150 ms |
28732 KB |
Output is correct |
7 |
Correct |
130 ms |
26484 KB |
Output is correct |
8 |
Correct |
175 ms |
35012 KB |
Output is correct |
9 |
Correct |
116 ms |
25564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
141 ms |
21216 KB |
Output is correct |
3 |
Correct |
185 ms |
34724 KB |
Output is correct |
4 |
Correct |
157 ms |
26236 KB |
Output is correct |
5 |
Correct |
150 ms |
27316 KB |
Output is correct |
6 |
Correct |
150 ms |
28732 KB |
Output is correct |
7 |
Correct |
130 ms |
26484 KB |
Output is correct |
8 |
Correct |
175 ms |
35012 KB |
Output is correct |
9 |
Correct |
116 ms |
25564 KB |
Output is correct |
10 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |