QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#486700#6629. Travelling TraderRafi220 1ms3824kbC++203.8kb2024-07-21 23:14:482024-07-21 23:14:48

Judging History

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

  • [2024-07-21 23:14:48]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:3824kb
  • [2024-07-21 23:14:48]
  • 提交

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 ;
	calc2(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) 
	{
		ans.pb(v);
		calc3(zkad3a[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;
}

详细

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3600kb

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: 3552kb

input:

2 2
2 1
243296356 635616793

output:

878913149
2
1 2 

result:

ok correct!

Test #13:

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

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 6 10 5 9 3 7 

result:

ok correct!

Test #14:

score: -7
Wrong Answer
time: 1ms
memory: 3824kb

input:

200 2
150 170
21 33
96 152
143 26
136 70
92 159
34 164
163 182
74 115
93 61
151 83
81 119
10 146
114 170
39 83
139 4
173 41
193 96
87 57
14 164
107 51
45 15
157 17
43 183
96 30
11 137
55 18
138 81
87 12
112 122
159 82
195 185
75 71
16 191
33 88
70 195
149 114
106 160
96 118
92 44
9 141
107 143
151 2...

output:

56346241078
98
1 89 194 179 151 39 83 135 27 125 180 120 117 122 33 114 28 110 171 149 170 59 96 105 131 193 21 88 162 94 138 45 25 78 62 199 36 127 129 15 99 72 12 159 41 67 173 186 42 116 44 24 178 17 92 82 197 101 5 32 53 70 76 121 87 29 198 64 93 19 37 100 3 141 8 126 9 108 52 61 54 14 137 49 75...

result:

wrong answer dist(110, 171) = 3 > 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%