#include<bits/stdc++.h>
using namespace std;
const int maxn=2000+10;
struct yal{
int u,v,wu,wv,jam;
int getad(int fu){
if(fu==u){
return v;
}
return u;
}
int getval(int fu){
if(fu==u){
return wu;
}
return wv;
}
}alle[maxn];
vector<int>adj[maxn];
int resr[maxn][maxn],kaf=0,res[maxn],suma,n;
set<pair<int,int>>dp[maxn];
void merge(int u,int v){
int fu=u;
if((int)dp[u].size()<(int)dp[v].size()){
swap(u,v);
}
for(auto x:dp[v]){
dp[u].insert(x);
}
if(fu!=u){
swap(dp[fu],dp[u]);
}
}
void dfs(int u,int p=-1,int len=0){
dp[u].clear();
if(p!=-1){
dp[u].insert(make_pair(0,u));
}
for(auto x:adj[u]){
int v=alle[x].getad(u);
if(v!=p){
//cout<<"ha: "<<u<<" "<<v<<" "<<len<<endl;
kaf+=alle[x].getval(u);
dfs(v,u,alle[x].getval(v));
//cout<<"salam: "<<u<<" "<<v<<endl;
merge(u,v);
//cout<<"injy vas mas "<<u<<" "<<dp[u].size()<<" "<<v<<" "<<dp[v].size()<<endl;
}
}
auto x=*dp[u].rbegin();
dp[u].erase(x);
x.first+=len;
dp[u].insert(x);
}
void solve(int u){
kaf=0;
dfs(u);
resr[u][0]=kaf;
for(int i=1;i<=n;i++){
resr[u][i]=resr[u][i-1];
if((int)dp[u].size()>0){
resr[u][i]+=(*dp[u].rbegin()).first;
dp[u].erase(*dp[u].rbegin());
}
res[i]=max(res[i],resr[u][i-1]);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0;i<n-1;i++){
cin>>alle[i].u>>alle[i].v>>alle[i].wv>>alle[i].wu;
alle[i].jam=alle[i].wu+alle[i].wv;
suma+=alle[i].wu+alle[i].wv;
adj[alle[i].u].push_back(i);
adj[alle[i].v].push_back(i);
}
for(int i=1;i<=n;i++){
solve(i);
}
int q;
cin>>q;
for(int i=1;i<=n;i++){
res[i]=suma-res[i];
}
for(int i=0;i<q;i++){
int d;
cin>>d;
cout<<res[d]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
612 KB |
Output is correct |
2 |
Runtime error |
2 ms |
856 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Runtime error |
1 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |