QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#68309 | #2065. Cyclic Distance | Komodo | WA | 1291ms | 30544kb | C++14 | 1.6kb | 2022-12-15 17:36:38 | 2022-12-15 17:36: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> 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+1;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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 29392kb
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: 1291ms
memory: 30544kb
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 617645 482310 3857594 -1986478 581968 1138283 -1189994 1982367 965915 3418504 5026562 1623463 3640836 1952191 1179067 1691929 3460234 1799312 58854 5749917 2353650 902988 -1169586 -1270464 1358983 1182231 2273662 -128478 192070 4313514 3907712 3031390 3104226 2419146 3214775 4197514 3956162 ...
result:
wrong answer 1st lines differ - expected: '1962986', found: '-336722'