QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#123783#2065. Cyclic Distanceunnamed7WA 30ms22580kbC++141.9kb2023-07-13 17:02:162023-07-13 17:02:20

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-13 17:02:20]
  • 评测
  • 测评结果:WA
  • 用时:30ms
  • 内存:22580kb
  • [2023-07-13 17:02:16]
  • 提交

answer

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
const int N=2e5+5;
const ll INF=1e18;
int t,n,k,u,v,w,h[N],tot;
ll ans;
struct edge
{
	int to,val,nxt;
}e[N<<1];
void add(int u,int v,int w) 
{
	e[++tot]={v,w,h[u]};
	h[u]=tot;
}
struct node
{
	priority_queue<ll,vector<ll>,greater<ll>> ql;
	priority_queue<ll> qr;
	ll cl,cr;
	int sz;
	void clear() 
	{
		while(ql.size()) ql.pop();
		while(qr.size()) qr.pop();
		cl=cr=0;sz=0;	
	} 
	void insert(ll x)
	{
		ql.push(x-cl);sz++;
		if((int)ql.size()>k/2)
		{
			qr.push(ql.top()+cl-cr);
			ql.pop();
		}
	}
	void sswap(node &b)
	{
		swap(ql,b.ql);swap(qr,b.qr);
		swap(cl,b.cl);swap(cr,b.cr);swap(sz,b.sz);
	}
	ll sum(int k) 
	{
		ll res=0;
		while(k--)
		{
			if(ql.size()) res+=ql.top()+cl,ql.pop();
			else res+=qr.top()+cr,qr.pop(); 
		}
		return res;
	}
	void out()
	{
		vector<int> v;
		while(ql.size()) v.push_back(ql.top()),ql.pop();
		reverse(begin(v),end(v));
		for(int u:v) ql.push(u),cout<<u+cl<<" ";
		v.clear();
		while(qr.size()) v.push_back(qr.top()),qr.pop(); 
		for(int u:v) qr.push(u),cout<<u+cr<<" ";
		cout<<"\n";
	}
}f[N];
void merge(node &a,node &b)
{
	if(a.sz<b.sz) a.sswap(b);
	while(b.ql.size()) a.insert(b.ql.top()+b.cl),b.ql.pop();
	while(b.qr.size()) a.insert(b.qr.top()+b.cr),b.qr.pop();
}
void dfs(int u,int fa) 
{
	f[u].insert(0);
	for(int i=h[u];i;i=e[i].nxt)
	{
		int v=e[i].to,w=e[i].val;
		if(v==fa) continue;
		dfs(v,u);f[v].cl+=w;f[v].cr-=w;
		if(f[v].qr.size()) 
		{
			ll t=f[v].qr.top();f[v].qr.pop();
			f[v].qr.push(t+w);	
		}  
		merge(f[u],f[v]);
	}
}
int main() 
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&k);tot=0;ans=0;
		for(int i=1;i<=n;i++) h[i]=0,f[i].clear();
		for(int i=1;i<n;i++) 
		{
			scanf("%d%d%d",&u,&v,&w);
			add(u,v,w),add(v,u,w);
		}
		dfs(1,0);ans=2*f[1].sum(k);
		printf("%lld\n",ans);
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 20936kb

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: 30ms
memory: 22580kb

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:

1962986
617612
1732662
3482488
4689260
4904192
1138234
3740590
1982318
965882
3418504
5026562
1623414
1885106
1952142
4012796
1691896
3102076
2380932
3076270
5697196
7258020
879020
2500212
3613854
1358950
1182198
2273662
2331560
1681964
8917452
2373066
3163042
3104226
4254774
3162310
5058442
3669186...

result:

wrong answer 6th lines differ - expected: '3823636', found: '4904192'