#include <bits/stdc++.h>
#define ll long long
#define int ll
#define pb push_back
#define S second
#define F first
#define pii pair<int,int>
#define vi vector <int>
#define vvi vector <vi>
#define vvvi vector <vvi>
#define vp vector <pii>
#define vvp vector <vp>
#define vb vector <bool>
#define vvb vector <vb>;
#define INF INT_MAX
#define MOD 1000000007
#define MAXN 200000
using namespace std;
pii val[MAXN+1];
int a[MAXN+1];
vector<vector<pair<int,pii>>> adj(MAXN+1);
int root;
void dfs(int v,int p){
val[v]={0,v};
for (auto X:adj[v]){
int to=X.F;
int x=X.S.F;
int y=X.S.S;
if (to==p)continue;
dfs(to,v);
val[v]=max(val[v],{val[to].F+x,val[to].S});
}
for (auto X:adj[v]){
int to=X.F;
int x=X.S.F;
int y=X.S.S;
if (to==p)continue;
if (val[to].S!=val[v].S || v==root)a[val[to].S]=val[to].F+x;
}
}
int calc(int v,int p){
int res=0;
for (auto X:adj[v]){
int to=X.F;
int x=X.S.F;
int y=X.S.S;
if (to==p)continue;
res+=calc(to,v)+y;
}
return res;
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
int cost=0;
for (int i=1;i<n;i++){
int u,v;
cin>>u>>v;
int x,y;
cin>>x>>y;
adj[u].pb({v,{x,y}});
adj[v].pb({u,{y,x}});
cost+=y+x;
}
int q;
cin>>q;
int e[q+1];
for (int i=1;i<=n;i++)cin>>e[i];
vi ans(q+1);
for (root=1;root<=n;root++){
for (int i=1;i<=n;i++)a[i]=0,val[i]={0,0};
int up=calc(root,0);
dfs(root,0);
vi V={INF};
for (int i=1;i<=n;i++)V.pb(a[i]);
sort(V.begin(),V.end());
reverse(V.begin(),V.end());
vi p(n+1);
for (int i=1;i<=n;i++)p[i]=p[i-1]+V[i];
for (int i=1;i<=q;i++){
ans[i]=max(ans[i],up+p[e[i]-1]);
}
}
for (int i=1;i<=q;i++)cout<<cost-ans[i]<<"\n";
}
Compilation message
designated_cities.cpp: In function 'void dfs(long long int, long long int)':
designated_cities.cpp:28:7: warning: unused variable 'y' [-Wunused-variable]
28 | int y=X.S.S;
| ^
designated_cities.cpp:36:7: warning: unused variable 'y' [-Wunused-variable]
36 | int y=X.S.S;
| ^
designated_cities.cpp: In function 'long long int calc(long long int, long long int)':
designated_cities.cpp:45:7: warning: unused variable 'x' [-Wunused-variable]
45 | int x=X.S.F;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Execution timed out |
2091 ms |
26672 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Execution timed out |
2090 ms |
26740 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7004 KB |
Output is correct |
2 |
Execution timed out |
2091 ms |
26672 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Incorrect |
2 ms |
7004 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |