#include<bits/stdc++.h>
using namespace std;
const long long maxn=200000+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 n,val[maxn],res[maxn],suma=0,kaf=0,valpre[maxn];
deque<long long>mxd[maxn];
void prea(long long u=1,long long p=-1){
for(auto x:adj[u]){
long long v=alle[x].getad(u);
long long wu=alle[x].getval(u);
if(v==p){
val[u]+=wu;
}
else{
val[1]+=wu;
val[v]-=wu;
prea(v,u);
}
}
}
void dfsa(long long u=1,long long p=-1){
if(p!=-1){
val[u]+=val[p];
}
for(auto x:adj[u]){
long long v=alle[x].getad(u);
if(v!=p){
dfsa(v,u);
}
}
}
void aval(){
prea();
dfsa();
long long mx=0;
for(long long i=1;i<=n;i++){
if(val[i]>=mx){
res[1]=val[i];
mx=val[i];
}
}
}
set<pair<long long,long long>>dp[maxn];
pair<long long,long long>stf[maxn];
long long timea=0;
void predfs(long long u,long long par=-1){
mxd[u].push_back(0);
mxd[u].push_back(0);
timea++;
stf[u].first=timea;
for(auto x:adj[u]){
long long v=alle[x].getad(u);
if(v!=par){
predfs(v,u);
mxd[u].push_back(mxd[v].back()+alle[x].getval(v));
sort(mxd[u].begin(),mxd[u].end());
mxd[u].pop_front();
}
}
stf[u].second=timea;
for(auto x:adj[u]){
long long v=alle[x].getad(u);
if(v==par){
valpre[stf[u].first]+=alle[x].getval(u);
valpre[stf[u].second+1]-=alle[x].getval(u);
valpre[0]+=alle[x].getval(v);
valpre[stf[u].first]-=alle[x].getval(v);
valpre[stf[u].second+1]+=alle[x].getval(v);
}
}
}
void dfsdown(long long u,long long par=-1,long long len=0){
mxd[u].push_back(len);
sort(mxd[u].begin(),mxd[u].end());
mxd[u].pop_front();
for(auto x:adj[u]){
long long v=alle[x].getad(u);
if(v!=par){
if(mxd[u].back()==mxd[v].back()+alle[x].getval(v)){
dfsdown(v,u,mxd[u][0]+alle[x].getval(u));
}
else{
dfsdown(v,u,mxd[u].back()+alle[x].getval(u));
}
}
}
}
long long getd(){
predfs(1);
dfsdown(1);
for(long long i=1;i<maxn;i++){
valpre[i]+=valpre[i-1];
}
long long rt=1,mx=0;
for(long long i=1;i<=n;i++){
if(mx<=valpre[stf[i].first]+mxd[i].back()){
rt=i;
mx=valpre[stf[i].first]+mxd[i].back();
}
}
return rt;
}
void merge(long long u,long long v){
long long fu=u;
if((long long)dp[u].size()<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){
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 dovombebad(){
long long g=getd();
//cout<<"wtf: "<<g<<endl;
dfs(g);
//cout<<"nagooo"<<endl;
long long ted=(long long)dp[g].size();
res[2]=kaf;
//cout<<"by"<<ted<<endl;
for(long long i=2;i<=ted;i++){
res[i]+=i!=2?res[i-1]:0+(*dp[g].rbegin()).first;
dp[g].erase(*dp[g].rbegin());
}
}
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);
}
long long q;
cin>>q;
aval();
if(n>1){
dovombebad();
}
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";
}
}
Compilation message
designated_cities.cpp: In function 'void merge(long long int, long long int)':
designated_cities.cpp:128:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | if((long long)dp[u].size()<dp[v].size()){
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
156316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
156436 KB |
Output is correct |
2 |
Correct |
825 ms |
246156 KB |
Output is correct |
3 |
Correct |
1055 ms |
240048 KB |
Output is correct |
4 |
Correct |
773 ms |
247996 KB |
Output is correct |
5 |
Correct |
810 ms |
217648 KB |
Output is correct |
6 |
Correct |
805 ms |
223356 KB |
Output is correct |
7 |
Correct |
709 ms |
209600 KB |
Output is correct |
8 |
Correct |
989 ms |
241780 KB |
Output is correct |
9 |
Correct |
542 ms |
203832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
76 ms |
156244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
156316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
156436 KB |
Output is correct |
2 |
Correct |
825 ms |
246156 KB |
Output is correct |
3 |
Correct |
1055 ms |
240048 KB |
Output is correct |
4 |
Correct |
773 ms |
247996 KB |
Output is correct |
5 |
Correct |
810 ms |
217648 KB |
Output is correct |
6 |
Correct |
805 ms |
223356 KB |
Output is correct |
7 |
Correct |
709 ms |
209600 KB |
Output is correct |
8 |
Correct |
989 ms |
241780 KB |
Output is correct |
9 |
Correct |
542 ms |
203832 KB |
Output is correct |
10 |
Incorrect |
76 ms |
156244 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
80 ms |
156316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |