QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#814501 | #2429. Conquer The World | dongyc666 | AC ✓ | 1203ms | 199368kb | C++17 | 1.5kb | 2024-12-14 17:56:49 | 2024-12-14 17:56:52 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int NR=3e5+10;
#define int long long
int n,a[NR],dep[NR],val[NR],sum[NR],idx[NR],ans;
#define pii pair<int,int>
#define fi first
#define se second
#define mkp make_pair
vector<pii>g[NR];
#define pb emplace_back
priority_queue<int,vector<int>,greater<int> >q1[NR],q2[NR];
void merge(int &x,int y,int d){
if(sum[x]<sum[y])swap(x,y);
while(!q1[y].empty())q1[x].push(q1[y].top()),q1[y].pop();
while(!q2[y].empty())q2[x].push(q2[y].top()),q2[y].pop();
sum[x]+=sum[y];
}
const int inf=1e12;
void dfs(int id,int fath){
idx[id]=id;sum[id]=abs(val[id]);
if(val[id]>0){
for(int i=1;i<=val[id];i++)q2[id].push(dep[id]);
}
if(val[id]<0){
for(int i=1;i<=-val[id];i++)q1[id].push(dep[id]-inf);
}
for(auto lcy:g[id])
if(lcy.fi!=fath){
dep[lcy.fi]=dep[id]+lcy.se;
dfs(lcy.fi,id);
merge(idx[id],idx[lcy.fi],dep[id]);
}
while(!q1[idx[id]].empty()&&!q2[idx[id]].empty()&&q1[idx[id]].top()+q2[idx[id]].top()-2*dep[id]<0){
int v1=q1[idx[id]].top(),v2=q2[idx[id]].top();
// cout<<v1<<" "<<v2<<" "<<dep[id]<<endl;
q1[idx[id]].pop(),q2[idx[id]].pop();
ans+=v1+v2-2*dep[id];
q1[idx[id]].push(2*dep[id]-v2),q2[idx[id]].push(2*dep[id]-v1);
}
}
signed main(){
cin>>n;
for(int i=1,x,y,z;i<n;i++)
cin>>x>>y>>z,g[x].pb(mkp(y,z)),g[y].pb(mkp(x,z));
for(int i=1,x,y;i<=n;i++)
cin>>x>>y,val[i]=x-y;
dfs(1,0);
int res=0;
for(int i=1;i<=n;i++)res+=max(0ll,-val[i]);
cout<<ans+inf*res<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 34528kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 34612kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 35104kb
Test #4:
score: 0
Accepted
time: 4ms
memory: 35220kb
Test #5:
score: 0
Accepted
time: 4ms
memory: 35556kb
Test #6:
score: 0
Accepted
time: 7ms
memory: 35464kb
Test #7:
score: 0
Accepted
time: 8ms
memory: 34972kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 35540kb
Test #9:
score: 0
Accepted
time: 27ms
memory: 50960kb
Test #10:
score: 0
Accepted
time: 56ms
memory: 51044kb
Test #11:
score: 0
Accepted
time: 32ms
memory: 50524kb
Test #12:
score: 0
Accepted
time: 6ms
memory: 35544kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 34940kb
Test #14:
score: 0
Accepted
time: 5ms
memory: 34000kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 34480kb
Test #16:
score: 0
Accepted
time: 3ms
memory: 35216kb
Test #17:
score: 0
Accepted
time: 7ms
memory: 35492kb
Test #18:
score: 0
Accepted
time: 19ms
memory: 36480kb
Test #19:
score: 0
Accepted
time: 27ms
memory: 41400kb
Test #20:
score: 0
Accepted
time: 229ms
memory: 82880kb
Test #21:
score: 0
Accepted
time: 209ms
memory: 50596kb
Test #22:
score: 0
Accepted
time: 201ms
memory: 51088kb
Test #23:
score: 0
Accepted
time: 244ms
memory: 52420kb
Test #24:
score: 0
Accepted
time: 127ms
memory: 46872kb
Test #25:
score: 0
Accepted
time: 149ms
memory: 45988kb
Test #26:
score: 0
Accepted
time: 244ms
memory: 52708kb
Test #27:
score: 0
Accepted
time: 460ms
memory: 100724kb
Test #28:
score: 0
Accepted
time: 373ms
memory: 81516kb
Test #29:
score: 0
Accepted
time: 605ms
memory: 115544kb
Test #30:
score: 0
Accepted
time: 975ms
memory: 174060kb
Test #31:
score: 0
Accepted
time: 891ms
memory: 158284kb
Test #32:
score: 0
Accepted
time: 788ms
memory: 137876kb
Test #33:
score: 0
Accepted
time: 696ms
memory: 128640kb
Test #34:
score: 0
Accepted
time: 939ms
memory: 154248kb
Test #35:
score: 0
Accepted
time: 284ms
memory: 63844kb
Test #36:
score: 0
Accepted
time: 364ms
memory: 75288kb
Test #37:
score: 0
Accepted
time: 1203ms
memory: 199368kb
Test #38:
score: 0
Accepted
time: 721ms
memory: 129372kb
Test #39:
score: 0
Accepted
time: 411ms
memory: 100948kb
Test #40:
score: 0
Accepted
time: 359ms
memory: 78780kb
Test #41:
score: 0
Accepted
time: 420ms
memory: 86708kb
Test #42:
score: 0
Accepted
time: 353ms
memory: 79032kb
Test #43:
score: 0
Accepted
time: 352ms
memory: 78576kb
Test #44:
score: 0
Accepted
time: 347ms
memory: 83816kb
Test #45:
score: 0
Accepted
time: 414ms
memory: 88928kb
Test #46:
score: 0
Accepted
time: 4ms
memory: 33512kb
Test #47:
score: 0
Accepted
time: 6ms
memory: 43592kb
Test #48:
score: 0
Accepted
time: 4ms
memory: 33552kb
Test #49:
score: 0
Accepted
time: 603ms
memory: 116216kb
Test #50:
score: 0
Accepted
time: 236ms
memory: 52768kb
Test #51:
score: 0
Accepted
time: 8ms
memory: 35496kb
Test #52:
score: 0
Accepted
time: 0ms
memory: 35436kb
Test #53:
score: 0
Accepted
time: 0ms
memory: 35236kb
Test #54:
score: 0
Accepted
time: 3ms
memory: 34468kb
Test #55:
score: 0
Accepted
time: 3ms
memory: 34400kb
Test #56:
score: 0
Accepted
time: 0ms
memory: 35216kb
Test #57:
score: 0
Accepted
time: 2ms
memory: 35608kb
Test #58:
score: 0
Accepted
time: 11ms
memory: 36408kb
Test #59:
score: 0
Accepted
time: 162ms
memory: 53124kb
Test #60:
score: 0
Accepted
time: 368ms
memory: 79944kb
Test #61:
score: 0
Accepted
time: 845ms
memory: 149772kb
Test #62:
score: 0
Accepted
time: 860ms
memory: 144276kb
Test #63:
score: 0
Accepted
time: 340ms
memory: 78148kb
Test #64:
score: 0
Accepted
time: 354ms
memory: 78868kb
Test #65:
score: 0
Accepted
time: 481ms
memory: 80388kb
Test #66:
score: 0
Accepted
time: 483ms
memory: 80488kb
Test #67:
score: 0
Accepted
time: 507ms
memory: 114748kb
Test #68:
score: 0
Accepted
time: 543ms
memory: 125144kb
Test #69:
score: 0
Accepted
time: 404ms
memory: 114820kb
Test #70:
score: 0
Accepted
time: 399ms
memory: 112640kb