//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 200015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,dp[N],fa[N],ans[N],mx[N],pos[N],total=0;
vector<int>g[N],leaf;
map<pii,int>cost;
void dfs1(int p,int lp){
if (g[p].size()==1){
leaf.push_back(p);
pos[p]=p;
}
for (auto i:g[p]){
if (i!=lp){
dfs1(i,p);
int cs=mx[i]+cost[{i,p}]+cost[{p,i}];
dp[p]=dp[p]+dp[i]+cost[{i,p}];
if (mx[p]<cs){
mx[p]=cs;
pos[p]=pos[i];
}
}
}
}
void dfs2(int p,int lp){
dp[p]=(dp[p]+dp[lp]+cost[{lp,p}]);
ans[1]=min(ans[1],total-dp[p]);
for (auto i:g[p]){
if (i!=lp){
dp[p]=dp[p]-dp[i]-cost[{i,p}];
dfs2(i,p);
dp[p]=dp[p]+dp[i]+cost[{i,p}];
}
}
dp[p]=(dp[p]-dp[lp]-cost[{lp,p}]);
}
signed main(){
fast
cin>>n;
for (int i=0;i<=n;i++){
ans[i]=1e18;
cost[{0,i}]=0; cost[{i,0}]=0;
}
for (int i=1;i<n;i++){
int a,b,c,d; cin>>a>>b>>c>>d;
total=(total+c+d);
g[a].push_back(b);
g[b].push_back(a);
cost[{a,b}]=c; cost[{b,a}]=d;
}
dfs1(1,0);
dfs2(1,0);
int q; cin>>q;
while (q--){
int c; cin>>c;
cout<<ans[c]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
1513 ms |
69908 KB |
Output is correct |
3 |
Correct |
1638 ms |
95720 KB |
Output is correct |
4 |
Correct |
1508 ms |
69836 KB |
Output is correct |
5 |
Correct |
1521 ms |
69828 KB |
Output is correct |
6 |
Correct |
1524 ms |
73672 KB |
Output is correct |
7 |
Correct |
1524 ms |
70244 KB |
Output is correct |
8 |
Correct |
1690 ms |
96620 KB |
Output is correct |
9 |
Correct |
1507 ms |
69300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
1513 ms |
69908 KB |
Output is correct |
3 |
Correct |
1638 ms |
95720 KB |
Output is correct |
4 |
Correct |
1508 ms |
69836 KB |
Output is correct |
5 |
Correct |
1521 ms |
69828 KB |
Output is correct |
6 |
Correct |
1524 ms |
73672 KB |
Output is correct |
7 |
Correct |
1524 ms |
70244 KB |
Output is correct |
8 |
Correct |
1690 ms |
96620 KB |
Output is correct |
9 |
Correct |
1507 ms |
69300 KB |
Output is correct |
10 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |