QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#68313 | #2065. Cyclic Distance | Komodo | WA | 1242ms | 30384kb | C++14 | 1.6kb | 2022-12-15 17:40:40 | 2022-12-15 17:40:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200020
int n,k;
struct seg{
priority_queue<ll,vector<ll>,greater<ll> > q;
ll tag;
void merge(seg &x){
if(x.q.size()>q.size())swap(q,x.q),swap(x.tag,tag);
while(x.q.size())q.push(x.q.top()+x.tag-tag),x.q.pop();
}
void clear(){while(q.size())q.pop();tag=0;}
};
struct conv{
seg s1,s2,s3;
void clear(){s1.clear(),s2.clear(),s3.clear();}
void maintain(){
while(s1.q.size()>k/2)s2.q.push(s1.q.top()),s1.q.pop();
while(s2.q.size()>k%2)s3.q.push(s2.q.top()),s2.q.pop();
}
void merge(conv &x){
s1.merge(x.s1),s2.merge(x.s2),s3.merge(x.s3);
}
}d[N];
struct edge{
int v,w,nxt;
}e[N<<1];
int head[N],tot=1;
void ae(int u,int v,int w){e[tot]=(edge){v,w,head[u]};head[u]=tot++;}
void dfs(int u,int f){
d[u].s1.q.push(-d[u].s1.tag);
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].v,w=e[i].w;if(v==f)continue;
dfs(v,u);
d[v].s1.tag+=w*2;d[v].s3.tag-=w*2;
d[u].merge(d[v]);
}
d[u].maintain();
}
void work(){
scanf("%d%d",&n,&k);
for(int i=1;i<n;i++){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
ae(u,v,w),ae(v,u,w);
}
dfs(1,0);
vector<ll> v;
while(d[1].s3.q.size())v.push_back(d[1].s3.q.top()+d[1].s3.tag),d[1].s3.q.pop();
while(d[1].s2.q.size())v.push_back(d[1].s2.q.top()+d[1].s2.tag),d[1].s2.q.pop();
while(d[1].s1.q.size())v.push_back(d[1].s1.q.top()+d[1].s1.tag),d[1].s1.q.pop();
reverse(v.begin(),v.end());
ll ans=0;
for(int i=0;i<=k;i++)ans+=v[i];
printf("%lld\n",ans);
for(int i=1;i<=n;i++)d[i].clear();
memset(head,0,sizeof(head));tot=1;
}
int main(){
int t;scanf("%d",&t);
while(t--)work();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 30384kb
input:
1 5 3 1 2 4 1 3 1 1 4 8 4 5 9
output:
44
result:
ok single line: '44'
Test #2:
score: -100
Wrong Answer
time: 1242ms
memory: 29564kb
input:
57206 3 2 1 2 574927 2 3 406566 2 2 1 2 308806 4 3 1 2 312588 2 3 500141 2 4 53602 3 3 1 2 797183 2 3 944061 5 3 1 2 624010 2 3 488613 3 4 734514 4 5 497493 5 4 1 2 540278 2 3 488655 2 4 228989 2 5 653896 2 2 1 2 569117 4 2 1 2 821764 2 3 499471 1 4 549060 2 2 1 2 991159 2 2 1 2 482941 5 4 1 2 30462...
output:
-336722 617612 482310 2857312 238768 581968 1138234 453534 1982318 965882 3418504 5026562 1623414 1885106 1952142 1126298 1691896 1177744 1799312 3067414 5697196 2353650 -544332 53680 357642 1358950 1182198 2273662 1543340 494920 4313514 2373066 3075274 3104226 3031022 3162310 4197514 3669186 626772...
result:
wrong answer 1st lines differ - expected: '1962986', found: '-336722'