QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#172563 | #2065. Cyclic Distance | KLPP# | TL | 38ms | 37064kb | C++20 | 2.2kb | 2023-09-09 19:45:31 | 2023-09-09 19:45:31 |
Judging History
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];
multiset<pair<lld,int> >S;
int exc;
lld test(int node){
exc=-1;
st(node);
S.clear();
rep(i,0,n)cnt[i]=0;
rep(i,0,n){
S.insert({-dist[i],lab[i]});
}
int tot=0;
lld ans=0;
while(!S.empty() && tot<k){
lld D=-S.begin()->first;
int v=S.begin()->second;
S.erase(S.begin());
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<=2)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: 37012kb
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: 0
Accepted
time: 38ms
memory: 36016kb
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 3823636 1138234 3740590 1982318 965882 3418504 5026562 1623414 1885106 1952142 3050630 1691896 3102076 2380932 3076270 5697196 7258020 879020 2500212 3613854 1358950 1182198 2273662 2331560 1681964 8917452 2373066 3163042 3104226 3642898 3162310 5058442 3669186...
result:
ok 57206 lines
Test #3:
score: 0
Accepted
time: 36ms
memory: 37064kb
input:
57087 3 3 1 2 34132 1 3 188096 2 2 1 2 996527 2 2 1 2 475736 5 3 1 2 329834 2 3 339687 1 4 954113 4 5 224354 2 2 1 2 641444 2 2 1 2 114059 5 3 1 2 635722 1 3 552627 1 4 721758 3 5 396156 4 3 1 2 655099 2 3 963393 1 4 953969 5 2 1 2 369719 1 3 22087 1 4 531252 3 5 449025 4 3 1 2 788498 1 3 220292 1 4...
output:
444456 1993054 951472 3695976 1282888 228118 4612526 5144922 2004728 3309502 2626844 3053048 3939444 3790784 2617770 38866 3033250 5707738 511666 403846 1923106 3331606 3447180 2329518 5656124 33582 2283312 3454982 110590 3125394 4517486 4522330 2352316 3966810 3463746 5181112 3089346 1260326 466418...
result:
ok 57087 lines
Test #4:
score: -100
Time Limit Exceeded
input:
33344 9 6 1 2 562996 1 3 312637 3 4 591016 1 5 811983 2 6 896220 3 7 854379 2 8 861166 1 9 672337 8 6 1 2 53530 1 3 712638 1 4 539356 1 5 179377 3 6 456495 2 7 730760 4 8 934379 3 3 1 2 87024 1 3 328551 3 3 1 2 664600 1 3 519786 5 4 1 2 374521 1 3 484148 2 4 501378 1 5 280691 10 3 1 2 676949 1 3 639...