QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#24175 | #2065. Cyclic Distance | Qingyu | WA | 64ms | 14096kb | C++20 | 3.1kb | 2022-03-27 15:42:46 | 2022-04-30 05:14:12 |
Judging History
answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#if ( _WIN32 || __WIN32__ || _WIN64 || __WIN64__ )
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif
#if ( _WIN32 || __WIN32__ || _WIN64 || __WIN64__ )
#define UNS64 "%I64u"
#else
#define UNS64 "%llu"
#endif
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=201000;
int n,u,v,w,rt,T,_,mt[N],cnt[N],k,pv[N],mark[N];
ll ans;
vector<PII> e[N];
vector<array<ll,3>> dis;
void dfs(int u,int f,int br,ll d) {
dis.pb({d,br,u});
for (auto p:e[u]) {
int v=p.fi;
if (v==f) continue;
dfs(v,u,br==rt?v:br,d+p.se);
}
}
void solve(int x) {
rt=x;
dis.clear();
dfs(rt,0,rt,0);
sort(all(dis)); reverse(all(dis));
T++;
ll tmp=0;
int pt=0;
for (auto x:dis) {
if (mt[x[1]]!=T) mt[x[1]]=T,cnt[x[1]]=0;
if (pt<k&&cnt[x[1]]<k/2) { pt++; cnt[x[1]]++; tmp+=x[0]; mark[x[2]]=T; }
}
if (pt==k&&tmp>ans) {
ans=tmp; rt=x;
rep(i,1,n+1) pv[i]=(mark[i]==T);
}
}
int q[N],f[N],vis[N],sz[N],ms[N];
int find(int u) {
int t=1;q[0]=u;f[u]=-1;
rep(i,0,t) {
u=q[i];
rep(j,0,e[u].size()) {
int v=e[u][j].fi;
if (!vis[v]&&v!=f[u]) f[q[t++]=v]=u;
}
ms[q[i]]=0;
sz[q[i]]=1;
}
for (int i=t-1;i>=0;i--) {
ms[q[i]]=max(ms[q[i]],t-sz[q[i]]);
if (ms[q[i]]*2<=t) return q[i];
sz[f[q[i]]]+=sz[q[i]];
ms[f[q[i]]]=max(ms[f[q[i]]],sz[q[i]]);
}
return 0;
}
void gao(int u) {
u=find(u);
solve(u);
T++;
int maj=-1;
rep(i,0,k) {
auto x=dis[i];
// printf("dd " INT64 " " INT64 " " INT64 "\n",x[0],x[1],x[2]);
if (mt[x[1]]!=T) mt[x[1]]=T,cnt[x[1]]=0;
cnt[x[1]]++;
if (cnt[x[1]]>k/2) maj=x[1];
}
vis[u]=1;
//fprintf(stderr,"%d %d\n",u,maj);
if (maj!=-1&&!vis[maj]) gao(maj);
}
pair<ll,int> dfs(int u,int f) {
int fv=pv[u];
ll mdis=0;
for (auto p:e[u]) {
int v=p.fi;
if (v==f) continue;
auto z=dfs(v,u);
if (z.se<k/2) z.fi+=p.se;
fv+=z.se;
mdis=max(mdis,z.fi);
}
if (pv[u]) mdis=-(1ll<<60);
return mp(mdis,fv);
}
void solve() {
scanf("%d%d",&n,&k);
rep(i,1,n+1) e[i].clear(),vis[i]=0;
ans=0;
for (int i=1;i<n;i++) {
scanf("%d%d%d",&u,&v,&w);
e[u].pb(mp(v,w));
e[v].pb(mp(u,w));
}
gao(1);
printf(INT64 "\n",2*ans);
}
int main() {
for (scanf("%d",&_);_;_--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 14096kb
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: 42ms
memory: 14096kb
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: 45ms
memory: 14052kb
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
Wrong Answer
time: 64ms
memory: 14096kb
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...
output:
12876734 9717058 831150 2368772 4030518 7963678 2135868 739728 11584454 1670128 3432160 5573124 1293282 3608364 8574290 6242670 10860048 4726106 4903320 9713590 5160212 5958260 14418122 1913782 1393854 5129544 9369494 11601220 4751232 1623938 8259790 3591252 5112182 4761950 5284034 13000192 4895040 ...
result:
wrong answer 19th lines differ - expected: '5661430', found: '4903320'