QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#172572#2065. Cyclic DistanceKLPP#WA 31ms37800kbC++202.2kb2023-09-09 19:48:092023-09-09 19:48:09

Judging History

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

  • [2023-09-09 19:48:09]
  • 评测
  • 测评结果:WA
  • 用时:31ms
  • 内存:37800kb
  • [2023-09-09 19:48:09]
  • 提交

answer

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
vector<pair<int,lld> >nei[1000000];
lld dist[1000000];
bool vis[1000000];
int lab[1000000];
int n,k;
void DFS(int node, lld d=0, int label=-1){
	dist[node]=d;
	vis[node]=true;
	lab[node]=label;
	trav(a,nei[node]){
		if(!vis[a.first]){
			if(label==-1)DFS(a.first,d+a.second,a.first);
			else DFS(a.first,d+a.second,label);
		}
	}
}
void st(int node){
	rep(i,0,n)vis[i]=false;
	DFS(node);
}
void reset(){
	rep(i,0,n)vis[i]=false;
}
int sz[1000000];
bool bl[1000000];
void getsz(int node){
	vis[node]=true;
	sz[node]=1;
	trav(a,nei[node]){
		if(!vis[a.first] && !bl[a.first]){
			getsz(a.first);
			sz[node]+=sz[a.first];
		}
	}
}
int getcent(int node, int treesz){
	trav(a,nei[node]){
		if(!bl[a.first] && sz[node]>sz[a.first] && sz[a.first]*2>treesz){
			return getcent(a.first,treesz);
		}
	}
	return node;
}
int cnt[1000000];
vector<pair<lld,int> >V;
int exc;
lld test(int node){
	exc=-1;
	st(node);
	V.clear();
	rep(i,0,n)cnt[i]=0;
	rep(i,0,n){
		V.push_back({-dist[i],lab[i]});
	}
	sort(V.begin(),V.end());
	int tot=0;
	lld ans=0;
	trav(a,V){
		if(tot==k)continue;
		lld D=-a.first;
		int v=a.second;
		if(2*(cnt[v]+1)<=k){
			cnt[v]++;
			tot++;
			ans+=2*D;
		}else{
			exc=v;
		}
	}
	return ans;
}
void solve(){
	cin>>n>>k;
	rep(i,0,n)nei[i].clear(),vis[i]=false,bl[i]=false;
	rep(i,0,n-1){
		int x,y;
		lld c;
		cin>>x>>y>>c;
		x--;y--;
		nei[x].push_back({y,c});
		nei[y].push_back({x,c});
	}
	lld ans=0;
	int ver=0;
	while(true){
		reset();
		getsz(ver);
		int S=sz[ver];
		if(S<=5)break;
		int cen=getcent(ver,S);
		ans=max(ans,test(cen));
		if(exc==-1){
			cout<<ans<<"\n";
			return;
		}
		bl[cen]=true;
		ver=exc;
	}
	rep(i,0,n){
		if(vis[i])ans=max(ans,test(i));
	}
	cout<<ans<<"\n";
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	cin>>tt;
	while(tt--){
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 31ms
memory: 37800kb

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
4673450
1138234
3740590
1982318
965882
4780220
5026562
1623414
1885106
1952142
4370954
1691896
3102076
2380932
3076270
7444696
8129016
879020
2500212
3613854
1358950
1182198
2273662
2331560
1681964
10145602
2373066
3163042
3104226
3642898
4693290
5667258
366918...

result:

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