#include<bits/stdc++.h>
using namespace std;
const long long maxn=2000+10;
struct yal{
long long u,v,wu,wv,jam;
long long getad(long long fu){
if(fu==u){
return v;
}
return u;
}
long long getval(long long fu){
if(fu==u){
return wu;
}
return wv;
}
}alle[maxn];
vector<long long>adj[maxn];
long long resr[maxn][maxn],kaf=0,res[maxn],suma,n,cnt=0;
set<pair<long long,long long>>dp[maxn];
void merge(long long u,long long v){
long long fu=u;
if((long long)dp[u].size()<(long long)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(long long u,long long p=-1,long long len=0){
dp[u].clear();
if(p!=-1){
dp[u].insert(make_pair(0,u));
}
for(auto x:adj[u]){
long long 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(long long u){
kaf=0;
dfs(u);
resr[u][0]=kaf;
for(long long i=1;i<=n;i++){
resr[u][i]=resr[u][i-1];
if((long long)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(long long 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(long long i=1;i<=n;i++){
solve(i);
}
for(long long i=1;i<=n;i++){
long long f=1;
for(long long j=1;j<=n;j++){
if(resr[i][j-1]==res[j]){
f=0;
}
}
if(f){
cnt++;
}
}
// if(cnt==0){
// assert(0);
// }
long long q;
cin>>q;
for(long long i=1;i<=n;i++){
res[i]=suma-res[i];
}
for(long long i=0;i<q;i++){
long long d;
cin>>d;
cout<<res[d]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
604 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 |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Execution timed out |
2101 ms |
32680 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
0 ms |
600 KB |
Output is correct |
13 |
Runtime error |
2 ms |
860 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |