QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#486695 | #6629. Travelling Trader | Rafi22 | 0 | 1ms | 3600kb | C++20 | 3.8kb | 2024-07-21 23:10:06 | 2024-07-21 23:10:06 |
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
int inf=1000000007;
ll infl=1000000000000000007;
ll mod=1000000007;
ll mod1=998244353;
const int N=207;
ll p[N];
vector<int>G[N];
int n;
ll DP0[N],DP1[N],DP2[N],DP3[N];
int zkad0a[N],zkad0b[N];
int zkad3a[N],zkad3b[N],zkad3c[N];
int zkad1[N];
int zkad2[N];
int O[N];
void dfs(int v,int o)
{
O[v]=o;
vector<int>X;
ll S=0;
for(auto u:G[v])
{
if(u==o) continue;
dfs(u,v);
X.pb(u);
S+=p[u];
}
int m=sz(X);
//policzmy DP0
DP0[v]=p[v];
FOR(i,0,m-1)
{
if(DP3[X[i]]+p[v]>=DP0[v])
{
DP0[v]=DP3[X[i]]+p[v];
zkad0a[v]=X[i];
zkad0b[v]=-1;
}
FOR(j,0,m-1)
{
if(i==j) continue;
ll T=DP1[X[i]]-p[X[i]]+DP0[X[j]]-p[X[j]]+S+p[v];
if(v==1) debug(X[i],X[j],T,DP1[X[i]],DP0[X[j]]);
if(T>=DP0[v])
{
DP0[v]=T;
zkad0a[v]=X[i];
zkad0b[v]=X[j];
}
}
}
//policzmy DP1
DP1[v]=p[v]+S;
for(auto u:X)
{
if(DP2[u]-p[u]+p[v]+S>=DP1[v])
{
DP1[v]=DP2[u]-p[u]+p[v]+S;
zkad1[v]=u;
}
}
//policzmy DP2
ll mx=0;
DP2[v]=p[v]+S;
for(auto u:X)
{
if(DP1[u]-p[u]>=mx)
{
mx=DP1[u]-p[u];
zkad2[v]=u;
}
}
DP2[v]+=mx;
//policzmy DP3
DP3[v]=p[v];
FOR(i,0,m-1)
{
if(DP3[X[i]]-p[X[i]]+p[v]+S>=DP3[v])
{
DP3[v]=DP3[X[i]]-p[X[i]]+p[v]+S;
zkad3a[v]=X[i];
zkad3b[v]=-1;
zkad3c[v]=-1;
}
FOR(j,0,m-1)
{
if(i==j) continue;
if(DP2[X[i]]+DP3[X[j]]+p[v]>=DP3[v])
{
DP3[v]=DP2[X[i]]+DP3[X[j]]+p[v];
zkad3a[v]=X[i];
zkad3b[v]=X[j];
zkad3c[v]=-1;
}
FOR(l,0,m-1)
{
if(i==l||j==l) continue;
ll T=DP2[X[i]]-p[X[i]]+DP1[X[j]]-p[X[j]]+DP0[X[l]]-p[X[l]]+S+p[v];
if(T>=DP3[v])
{
DP3[v]=T;
zkad3a[v]=X[i];
zkad3b[v]=X[j];
zkad3c[v]=X[l];
}
}
}
}
debug(v,DP0[v],DP1[v],DP2[v],DP3[v]);
}
vector<int>ans;
void calc0(int v);
void calc1(int v);
void calc2(int v);
void calc3(int v);
void calc0(int v)
{
if(v==0) return ;
ans.pb(v);
if(zkad0b[v]==-1) calc3(zkad0a[v]);
else
{
calc1(zkad0a[v]);
for(auto u:G[v]) if(u!=O[v]&&u!=zkad0a[v]&&u!=zkad0b[v]) ans.pb(u);
calc0(zkad0b[v]);
}
}
void calc1(int v)
{
if(v==0) return ;
calc1(zkad1[v]);
for(auto u:G[v]) if(u!=O[v]&&u!=zkad1[v]) ans.pb(u);
ans.pb(v);
}
void calc2(int v)
{
if(v==0) return ;
ans.pb(v);
calc1(zkad2[v]);
for(auto u:G[v]) if(u!=O[v]&&u!=zkad2[v]) ans.pb(u);
}
void calc3(int v)
{
if(v==0) return ;
if(zkad3b[v]==-1)
{
calc3(zkad3a[v]);
ans.pb(v);
}
else if(zkad3c[v]==-1)
{
calc2(zkad3a[v]);
ans.pb(v);
calc3(zkad3b[v]);
}
else
{
calc2(zkad3a[v]);
ans.pb(v);
calc1(zkad3b[v]);
for(auto u:G[v]) if(u!=O[v]&&u!=zkad3a[v]&&u!=zkad3b[v]&&u!=zkad3c[v]) ans.pb(u);
calc0(zkad3c[v]);
}
}
void k2()
{
dfs(1,0);
calc0(1);
//cout<<DP0[1]<<endl;
cout<<DP0[1]<<endl<<sz(ans)<<endl;
for(auto x:ans) cout<<x<<" ";
cout<<endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k;
cin>>n>>k;
FOR(i,1,n-1)
{
int a,b;
cin>>a>>b;
G[a].pb(b);
G[b].pb(a);
}
FOR(i,1,n) cin>>p[i];
if(k==2) k2();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3456kb
input:
2 1 1 2 255959470 961356354
output:
result:
wrong output format Unexpected end of file - int64 expected
Subtask #2:
score: 0
Wrong Answer
Test #12:
score: 7
Accepted
time: 0ms
memory: 3564kb
input:
2 2 2 1 243296356 635616793
output:
878913149 2 1 2
result:
ok correct!
Test #13:
score: -7
Wrong Answer
time: 1ms
memory: 3600kb
input:
10 2 6 4 3 7 5 10 6 10 8 2 3 9 3 5 4 2 1 4 2 4 2 5 5 4 2 3 4 2
output:
33 10 1 2 8 4 9 3 7 5 10 6
result:
wrong answer dist(4, 9) = 5 > k = 2
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Runtime Error
Test #83:
score: 0
Runtime Error
input:
2000 3 1359 90 1703 163 158 188 360 1501 195 664 1414 215 1546 1756 536 1096 1726 1223 1150 104 1757 703 1982 282 1023 998 1180 419 576 1759 1496 1993 44 670 1703 952 855 849 1998 1399 1280 980 1533 1090 1270 678 1680 387 469 1734 1799 263 473 588 303 226 5 295 1489 1471 1094 1667 1912 210 1368 1360...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #5:
0%