답안 #699543

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699543 2023-02-17T10:14:06 Z anonimy Roadside Advertisements (NOI17_roadsideadverts) C++14
0 / 100
119 ms 20548 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;
vector<vector<pll>> st;
ll logn;
const int Q_VERT = 5;

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

	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 depth[v] < depth[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);
	dfs(0, 0, 0);

	logn = getlog(n);

	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:22: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]
   22 |  for (ll i = 0; i < tree[v].size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~
roadsideadverts.cpp: In function 'll LCA5(std::vector<long long int>&)':
roadsideadverts.cpp:67: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]
   67 |  for (ll i = 1; i < locations.size(); i++)
      |                 ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 119 ms 20548 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 30 ms 19276 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -