QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#625417#6107. One Pathstrlen_s_WA 14ms98096kbC++172.1kb2024-10-09 19:10:582024-10-09 19:10:59

Judging History

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

  • [2024-10-09 19:10:59]
  • 评测
  • 测评结果:WA
  • 用时:14ms
  • 内存:98096kb
  • [2024-10-09 19:10:58]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2005;
const ll inf=1e18;
int h[N],nex[N<<1],to[N<<1],e[N<<1],tot;
ll dp[N][N][3],ans[N];
ll g[N][3],f[N][3];
int siz[N];
int d[N],rt;
void add(int x,int y,int z){to[++tot]=y,e[tot]=z,nex[tot]=h[x],h[x]=tot;}
void dfs(int u,int fa){
  siz[u]=1;
  if(d[u]==1){dp[u][0][1]=dp[u][1][2]=dp[u][1][0]=0;return;}
  for(int i=h[u];i;i=nex[i]){
    int v=to[i];
    if(v==fa)continue;
    dfs(v,u);
    siz[u]+=siz[v];
  }
  g[0][1]=0;
  for(int i=h[u],sz=0;i;i=nex[i]){
    int v=to[i];
    if(v==fa)continue;
    for(int j=0;j<=sz;j++)f[j][0]=g[j][0],f[j][1]=g[j][1],g[j][0]=g[j][1]=-inf;
    for(int j=0;j<=sz;j++)
      for(int k=0;k<=siz[v];k++){
        ll x=max(dp[v][k][1],dp[v][k][0]+e[i]);
        g[j+k][0]=max({g[j+k][0],f[j][0]+x,f[j][1]+dp[v][k][0]});
        g[j+k][1]=max(g[j+k][1],f[j][1]+x);
      }
    sz+=siz[v];
  }
  for(int i=0;i<=siz[u];i++)dp[u][i][0]=g[i][0],dp[u][i][1]=g[i][1],g[i][0]=g[i][1]=-inf;
  g[0][0]=0;
  for(int i=h[u],sz=0;i;i=nex[i]){
    int v=to[i],w=e[i];
    if(v==fa)continue;
    for(int j=0;j<=sz;j++)
      for(int k=0;k<3;k++)
        f[j][k]=g[j][k],g[j][k]=-inf;
    for(int j=0;j<=sz;j++)
      for(int k=0;k<=siz[v];k++){
        ll x=max(dp[v][k][1],dp[v][k][0]+w);
        g[j+k][0]=max(g[j+k][0],f[j][0]+x);
        g[j+k][1]=max({g[j+k][1],f[j][1]+x,f[j][0]+dp[v][k][2]+w});
        g[j+k][2]=max({g[j+k][2],f[j][2]+x,f[j][1]+dp[v][k][2]+w});
      }
    sz+=siz[v];
  }
  for(int i=0;i<=siz[u];i++)dp[u][i][0]=max(dp[u][i][0],g[i+1][2]),dp[u][i][2]=g[i][1],g[i][0]=g[i][1]=g[i][2]=-inf;
}
int n,k;
signed main(){
  ios::sync_with_stdio(0);
  cin.tie(0),cout.tie(0);
  cin>>n>>k;
  for(int i=1,x,y,z;i<n;i++){
    cin>>x>>y>>z;
    add(x,y,z),add(y,x,z);
    d[x]++,d[y]++;
  }
  if(n==2){
    for(int i=0;i<=k;i++)cout<<e[1]<<' ';cout<<'\n';
    return 0;
  }
  rt=1;while(d[rt]==1)rt++;
  memset(dp,0xcf,sizeof(dp));memset(g,0xcf,sizeof(g));
  dfs(rt,0);
  for(int i=0;i<=k;i++)ans[i]=max((i?ans[i-1]:-inf),dp[rt][i+1][0]);
  for(int i=0;i<=k;i++)cout<<ans[i]<<' ';cout<<'\n';
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 97880kb

input:

5 1
1 3 2
4 5 4
3 4 3
2 3 7

output:

14 16 

result:

ok 2 number(s): "14 16"

Test #2:

score: 0
Accepted
time: 0ms
memory: 97884kb

input:

7 2
1 2 4
2 3 6
2 4 2
4 5 5
2 6 1
4 7 3

output:

13 20 21 

result:

ok 3 number(s): "13 20 21"

Test #3:

score: 0
Accepted
time: 8ms
memory: 98004kb

input:

50 2000
3 34 1
37 39 58720256
17 24 14680064
28 39 1
25 38 1
21 29 1
3 30 1
26 36 1
5 48 14336
4 22 1
26 41 1
41 44 1
5 14 1
23 25 28672
40 41 224
27 39 1
4 20 7340032
7 47 939524096
11 46 114688
30 49 3584
34 44 1
7 35 1
10 29 1
27 33 29360128
16 36 56
8 28 1
13 38 57344
34 45 896
15 35 469762048
1...

output:

1409286145 1761607683 1849688069 1871708167 1877213193 1878589451 1878933517 1879019535 1879041041 1879046419 1879047765 1879048103 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 1879048160 187...

result:

ok 2001 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 97920kb

input:

10 5
1 4 10
3 7 138
1 9 162
4 10 113
4 6 12
5 6 171
2 10 31
7 8 12
7 10 132

output:

566 769 781 781 781 781 

result:

ok 6 numbers

Test #5:

score: 0
Accepted
time: 0ms
memory: 3664kb

input:

2 0
1 2 340241821

output:

340241821 

result:

ok 1 number(s): "340241821"

Test #6:

score: 0
Accepted
time: 14ms
memory: 98096kb

input:

2000 0
450 1620 747893383
103 1602 466748018
746 1996 468886757
430 764 455748265
1201 1275 111041658
244 802 715844971
611 899 125646661
1105 1633 478144612
180 573 823370712
161 1018 67225549
1512 1915 538711443
829 1871 761057722
1297 1499 576790037
1492 1832 983172784
1486 1902 78076400
1206 121...

output:

83343544566 

result:

ok 1 number(s): "83343544566"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

2 2000
1 2 128840350

output:

128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 128840350 ...

result:

ok 2001 numbers

Test #8:

score: -100
Wrong Answer
time: 3ms
memory: 98012kb

input:

1139 1252
115 643 947372561
358 529 121247876
22 177 680088711
70 692 912515870
602 1032 172209848
908 1064 871258945
1052 1085 268011860
88 405 978751838
65 913 486052321
75 496 113634888
654 841 834034656
209 409 142094069
674 700 147589677
828 885 666263686
486 685 480409259
111 839 151780996
634...

output:

59768151302 90240365913 110773200826 126071855751 138635934480 150183113228 161491697716 171973479232 180896150286 189526447107 198151654732 205944843996 212594288075 218867722405 224758314304 230517398967 236194246382 241771608481 247290975973 252679174462 257726719571 262623730494 267497011096 272...

result:

wrong answer 94th numbers differ - expected: '466456439612', found: '466445836003'