Submission #699547

# Submission time Handle Problem Language Result Execution time Memory
699547 2023-02-17T10:30:13 Z anonimy Roadside Advertisements (NOI17_roadsideadverts) C++14
100 / 100
157 ms 23296 KB
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

vector<vector<pll>> tree;
vector<pll> par;
vector<ll> depth, tin;
vector<vector<pll>> st;
ll logn;
const int Q_VERT = 5;
ll t = 0;

void dfs(ll v, ll p, ll wp)
{
	par[v] = { p, wp };
	depth[v] = depth[p] + 1;
	tin[v] = t++;

	for (ll i = 0; i < tree[v].size(); i++)
	{
		ll u = tree[v][i].first, w = tree[v][i].second;

		if (u == p) continue;

		dfs(u, v, w);
	}
}

ll getlog(ll n)
{
	ll ans = 0;
	while (n) ans++, n >>= 1;
	
	return ans;
}

bool comp(ll v, ll u)
{
	return tin[v] < tin[u];
}

ll LCA(ll v, ll u)
{
	if (depth[v] < depth[u]) swap(v, u);
	
	ll diff = depth[v] - depth[u];
	for (ll i = 0; diff; i++, diff >>= 1)
		if (diff & 1)
			v = st[i][v].first;

	for (ll i = logn - 1; i >= 0; i--)
		if (st[i][v].first != st[i][u].first)
			v = st[i][v].first, u = st[i][u].first;

	if (u != v)
		v = st[0][v].first;

	return v;
}

ll LCA5(vector<ll>& locations)
{
	ll ans = locations[0];
	for (ll i = 1; i < locations.size(); i++)
		ans = LCA(ans, locations[i]);

	return ans;
}

ll price(ll v, ll u)
{
	if (depth[v] < depth[u]) swap(v, u);

	ll diff = depth[v] - depth[u];
	ll ans = 0;
	for (ll i = 0; diff; i++, diff >>= 1)
		if (diff & 1)
			 ans+=st[i][v].second, v = st[i][v].first;

	return ans;
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	ll n;
	cin >> n;

	tree.resize(n);
	for (ll i = 0; i < n - 1; i++)
	{
		ll v, u, w;
		cin >> v >> u >> w;

		tree[v].push_back({ u, w });
		tree[u].push_back({ v, w });
	}

	par.resize(n);
	depth.resize(n, 0);
	tin.resize(n);
	dfs(0, 0, 0);

	logn = getlog(n) + 1;

	st.resize(logn, vector<pll>(n));
	for (ll i = 0; i < n; i++)
		st[0][i] = par[i];

	for(ll i=1;i<logn;i++)
		for (ll j = 0; j < n; j++)
		{
			st[i][j].first = st[i - 1][st[i - 1][j].first].first;
			st[i][j].second = st[i - 1][j].second + st[i - 1][st[i - 1][j].first].second;
		}

	ll q;
	cin >> q;


	while (q--)
	{
		vector<ll> locations(Q_VERT);
		for (ll i = 0; i < Q_VERT; i++)
			cin >> locations[i];

		sort(locations.begin(), locations.end(), comp);

		ll root = LCA5(locations);

		ll ans = price(root, locations[0]);
		for (ll i = 1; i < Q_VERT; i++)
			ans += price(locations[i], LCA(locations[i - 1], locations[i]));

		cout << ans << "\n";
	}
}

Compilation message

roadsideadverts.cpp: In function 'void dfs(ll, ll, ll)':
roadsideadverts.cpp:24:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |  for (ll i = 0; i < tree[v].size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~
roadsideadverts.cpp: In function 'll LCA5(std::vector<long long int>&)':
roadsideadverts.cpp:69:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |  for (ll i = 1; i < locations.size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 114 ms 20724 KB Output is correct
2 Correct 113 ms 23092 KB Output is correct
3 Correct 122 ms 23048 KB Output is correct
4 Correct 140 ms 23108 KB Output is correct
5 Correct 123 ms 23296 KB Output is correct
6 Correct 118 ms 23160 KB Output is correct
7 Correct 142 ms 23140 KB Output is correct
8 Correct 136 ms 23116 KB Output is correct
9 Correct 123 ms 23148 KB Output is correct
10 Correct 115 ms 23116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 19708 KB Output is correct
2 Correct 34 ms 22476 KB Output is correct
3 Correct 44 ms 22536 KB Output is correct
4 Correct 40 ms 22604 KB Output is correct
5 Correct 34 ms 22516 KB Output is correct
6 Correct 35 ms 22472 KB Output is correct
7 Correct 36 ms 22596 KB Output is correct
8 Correct 38 ms 22444 KB Output is correct
9 Correct 37 ms 22476 KB Output is correct
10 Correct 37 ms 22476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 114 ms 20724 KB Output is correct
3 Correct 113 ms 23092 KB Output is correct
4 Correct 122 ms 23048 KB Output is correct
5 Correct 140 ms 23108 KB Output is correct
6 Correct 123 ms 23296 KB Output is correct
7 Correct 118 ms 23160 KB Output is correct
8 Correct 142 ms 23140 KB Output is correct
9 Correct 136 ms 23116 KB Output is correct
10 Correct 123 ms 23148 KB Output is correct
11 Correct 115 ms 23116 KB Output is correct
12 Correct 43 ms 19708 KB Output is correct
13 Correct 34 ms 22476 KB Output is correct
14 Correct 44 ms 22536 KB Output is correct
15 Correct 40 ms 22604 KB Output is correct
16 Correct 34 ms 22516 KB Output is correct
17 Correct 35 ms 22472 KB Output is correct
18 Correct 36 ms 22596 KB Output is correct
19 Correct 38 ms 22444 KB Output is correct
20 Correct 37 ms 22476 KB Output is correct
21 Correct 37 ms 22476 KB Output is correct
22 Correct 110 ms 21740 KB Output is correct
23 Correct 80 ms 20652 KB Output is correct
24 Correct 124 ms 22732 KB Output is correct
25 Correct 154 ms 22760 KB Output is correct
26 Correct 136 ms 22732 KB Output is correct
27 Correct 113 ms 22744 KB Output is correct
28 Correct 133 ms 22732 KB Output is correct
29 Correct 157 ms 22732 KB Output is correct
30 Correct 119 ms 22740 KB Output is correct
31 Correct 121 ms 22772 KB Output is correct