QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#123529 | #2065. Cyclic Distance | KevinLive | WA | 684ms | 26276kb | C++14 | 2.3kb | 2023-07-12 19:43:43 | 2023-07-12 19:43:45 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define ll long long
using namespace std;
const int N=2e5+10;
template<class T>
inline void read(T &x)
{
x=0;int f=0;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
if(f)x=~x+1;
}
template<class T,class ...T1>
inline void read(T &x,T1 &...x1)
{
read(x),read(x1...);
}
int T,n,k;
int tot,head[N],ver[N<<1],e[N<<1],ne[N<<1];
struct Heap
{
priority_queue<ll,vector<ll>,greater<ll> >h1,h2;
ll v,tag1,tag2;
Heap() : v(-1),tag1(0),tag2(0){}
int size(){return h1.size()+h2.size()+(v!=-1);}
void add(ll val)
{
if(h1.size()<k/2)return h1.push(val-tag1);
if(val>h1.top()+tag1)
{
ll ne=h1.top()+tag1;
return h1.pop(),h1.push(val-tag1),add(ne);
}
if((k&1)&&v==-1)return v=val,void();
if((k&1)&&val>v)
{
ll ne=v;
return v=val,add(ne);
}
if(h2.size()<k/2)return h2.push(val-tag2);
if(val>h2.top()+tag2)h2.pop(),h2.push(val-tag2);
}
}f[N];
inline void add(int u,int v,int w)
{
ver[++tot]=v;
e[tot]=w;
ne[tot]=head[u];
head[u]=tot;
}
inline void init()
{
tot=0;
memset(head,0,sizeof(head));
}
inline void merge(Heap &x,Heap &y)
{
if(x.size()<y.size())swap(x,y);
while(!y.h1.empty())x.add(y.h1.top()+y.tag1),y.h1.pop();
while(!y.h2.empty())x.add(y.h2.top()+y.tag2),y.h2.pop();
if(~y.v)x.add(y.v),y.v=-1;
}
void dfs(int x,int fa)
{
f[x].add(0);
for(int i=head[x];i;i=ne[i])
{
int y=ver[i];
if(y==fa)continue;
dfs(y,x);
f[y].tag1+=e[i]<<1,f[y].tag2-=e[i]<<1;
merge(f[x],f[y]);
}
}
inline void solve()
{
init();
read(n,k);
for(int i=1,u,v,w;i<n;i++)read(u,v,w),add(u,v,w),add(v,u,w);
dfs(1,0);
ll ans=0;
while(!f[1].h1.empty())ans+=f[1].h1.top(),f[1].h1.pop();
while(!f[1].h2.empty())ans+=f[1].h2.top(),f[1].h2.pop();
if((k&1)&&(~f[1].v))ans+=f[1].v,f[1].v=-1;
printf("%lld",ans);
}
int main()
{
// freopen("2.in","r",stdin);
read(T);
// T=1;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 26276kb
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: 684ms
memory: 26032kb
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:
196298661761217326623482488468926038236361138234374059019823189658823418504502656216234141885106195214230506301691896310207623809323076270569719672580208790202500212361385413589501182198227366223315601681964891745223730663163042310422636428983162310505844236691866267722283820199986621009662385474501...
result:
wrong answer 1st lines differ - expected: '1962986', found: '196298661761217326623482488468...4888587242578691226229041415532'