QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#78206 | #5421. Factories Once More | JinTianHao | WA | 24ms | 105000kb | C++17 | 2.1kb | 2023-02-17 11:50:27 | 2023-02-17 11:50:29 |
Judging History
answer
#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
// #define int long long
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
using namespace std;
template <typename T> void read(T &x){
x=0;char ch=getchar();int fh=1;
while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
x*=fh;
}
template <typename T> void write(T x) {
if (x<0) x=-x,putchar('-');
if (x>9) write(x/10);
putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
int n,m;
vector<pair<int,int>> g[100005];
namespace bp1
{
int dp[5005][5005],tmp[5005],siz[5005];
void dfs(int x,int fa)
{
dp[x][0]=dp[x][1]=0;siz[x]=1;
for(auto i:g[x])
{
if(i.first==fa) continue;
dfs(i.first,x);
for(int j=0;j<=min(siz[x]+siz[i.first],m);++j)
tmp[j]=-inf;
for(int j=0;j<=min(siz[x],m);++j)
for(int k=0;k<=min(siz[i.first],m-j);++k)
tmp[j+k]=max(tmp[j+k],dp[x][j]+dp[i.first][k]+i.second*k*(m-k));
siz[x]+=siz[i.first];
for(int j=0;j<=min(siz[x],m);++j)
dp[x][j]=tmp[j];
}
}
void solve()
{
memset(dp,-0x3f,sizeof(dp));
dfs(1,0);
writeln(dp[1][m]);
}
}
namespace bp2
{
int dp[100005][3],tmp[3],siz[100005];
void dfs(int x,int fa)
{
dp[x][0]=dp[x][1]=0;siz[x]=1;
for(auto i:g[x])
{
if(i.first==fa) continue;
dfs(i.first,x);
for(int j=0;j<=min(siz[x]+siz[i.first],m);++j)
tmp[j]=-inf;
for(int j=0;j<=min(siz[x],m);++j)
for(int k=0;k<=min(siz[i.first],m-j);++k)
tmp[j+k]=max(tmp[j+k],dp[x][j]+dp[i.first][k]+i.second*k*(m-k));
siz[x]+=siz[i.first];
for(int j=0;j<=min(siz[x],m);++j)
dp[x][j]=tmp[j];
}
}
void solve()
{
memset(dp,-0x3f,sizeof(dp));
dfs(1,0);
writeln(dp[1][m]);
}
}
signed main()
{
// freopen("bigdata.in","r",stdin);
// freopen("bigdata.out","w",stdout);
read(n);read(m);
for(int i=1;i<n;++i)
{
int a,b,c;
read(a);read(b);read(c);
g[a].push_back({b,c});
g[b].push_back({a,c});
}
if(n<=5000) bp1::solve();
else if(m==2) bp2::solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 9ms
memory: 105000kb
input:
6 3 1 2 3 2 3 2 2 4 1 1 5 2 5 6 3
output:
22
result:
ok 1 number(s): "22"
Test #2:
score: 0
Accepted
time: 19ms
memory: 105000kb
input:
4 3 1 2 2 1 3 3 1 4 4
output:
18
result:
ok 1 number(s): "18"
Test #3:
score: 0
Accepted
time: 4ms
memory: 103760kb
input:
2 2 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 24ms
memory: 10048kb
input:
100000 17 37253 35652 9892 56367 53643 1120 47896 49255 4547 93065 88999 1745 5251 6742 5031 49828 50972 8974 31548 46729 1032 56341 56287 4812 21896 22838 1682 82124 90557 7307 76289 76949 7028 33834 45380 6856 15499 15064 2265 10127 5251 9920 87208 93945 9487 68990 72637 6891 91640 85004 2259 4748...
output:
result:
wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements