Submission #240125

# Submission time Handle Problem Language Result Execution time Memory
240125 2020-06-18T06:43:18 Z MrRobot_28 Dostavljač (COCI18_dostavljac) C++17
0 / 140
157 ms 4480 KB
#include <bits/stdc++.h>
                  
using namespace std;
#define int long long
vector <vector <int> > g;
vector <int> a;
vector <int> h;
vector <vector <int> > pred;
vector <int> tin, tout;
bool pr(int a, int b)
{
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int len(int a, int b)
{
	if(pr(a, b))
	{
		return h[b] - h[a];
	}
	else if(pr(b, a))
	{
		return h[a] - h[b];
	}
	else
	{
		int sum = h[a] + h[b];
		for(int j = pred.size() - 1; j >= 0; j--)
		{
			if(!pr(pred[j][a], b))
			{
				a = pred[j][a];
			}
		}
		a = pred[0][a];
		return sum - 2 * h[a];
	}
}
int timer = 0;
vector <int> eiler;
void dfs(int v, int p = -1)
{
	tin[v] = timer++;
	for(int i = 0; i< g[v].size(); i++)
	{
		int to = g[v][i];
		if(to != p)
		{
			h[to] = h[v] + 1;
			pred[0][to] = v;
			dfs(to, v);
		}
	}
	eiler.push_back(v);
	tout[v] = timer++;
}
signed main(){
//	ios_base::sync_with_stdio(false);
//	cin.tie(NULL);
//	cout.tie(NULL);
	int n, m;
	cin >> n >> m;
	a.resize(n);
	g.resize(n);
	h.resize(n);
	tin.resize(n);
	tout.resize(n);
	int t = log2(n) + 1;
	pred.resize(t, vector <int> (n));
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for(int i = 0; i < n - 1; i++)
	{
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	dfs(0);
	reverse(eiler.begin(), eiler.end());
	for(int i = 1; i < t; i++){
		for(int j = 0; j < n; j++)
		{
			pred[i][j] = pred[i - 1][pred[i - 1][j]];
		}
	}
	vector <vector <int> > dist(n, vector <int> (n));
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++)
		{
			dist[i][j] = len(i, j);
		}
	}
	vector <vector <int> > dp(n, vector <int> (m + 1, -1e10));
	dp[0][0] = 0;
	dp[0][1] = a[0];
	int ans = 0;
	ans = a[0];
	for(int i = 1; i < n; i++)
	{
		for(int j = 0; j < i; j++)
		{
			int d = dist[eiler[i]][eiler[j]];
			for(int t = 0; t + d <= m; t++)
			{
				if(t + d  + 1 <= m)
				{
					dp[i][t + d + 1] = max(dp[i][t + d  + 1], dp[j][t] + a[eiler[i]]);
				}
				dp[i][t + d] = max(dp[i][t + d], dp[j][t]);
			}
		}
		for(int t = 0; t <= m; t++)
		{
			ans = max(ans, dp[i][t]);
		}
	}
	cout << ans;
	return 0;
}

Compilation message

dostavljac.cpp: In function 'void dfs(long long int, long long int)':
dostavljac.cpp:43:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i< g[v].size(); i++)
                 ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 384 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 436 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 640 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 640 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 24 ms 1280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 1656 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 86 ms 3064 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 157 ms 4480 KB Output isn't correct
2 Halted 0 ms 0 KB -