Submission #529330

# Submission time Handle Problem Language Result Execution time Memory
529330 2022-02-22T19:17:42 Z LucaDantas Factories (JOI14_factories) C++17
100 / 100
7199 ms 190328 KB
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;

constexpr int maxn = 5e5+10, logn = 21;
constexpr long long inf = 0x3f3f3f3f3f3f3f3f;

vector<pair<int,long long>> g[maxn];

int in[maxn], pai[logn][maxn], profundidade[maxn], t, n;
long long depth[maxn];

void dfs(int u) {
	in[u] = ++t;
	for(auto [v, w] : g[u])
		if(v != pai[0][u])
			pai[0][v] = u, depth[v] = depth[u] + w, profundidade[v] = profundidade[u] + 1, dfs(v);
}

void build_binary_lifting() {
	for(int l = 1; l < logn; l++) {
		for(int i = 1; i < maxn; i++) {
			pai[l][i] = pai[l-1][pai[l-1][i]];
		}
	}
}

int LCA(int a, int b) {
	if(profundidade[a] < profundidade[b]) swap(a, b);
	for(int l = logn-1; l >= 0; l--) {
		if(profundidade[a] - (1 << l) >= profundidade[b])
			a = pai[l][a];
	}
	if(a == b) return a;
	for(int l = logn-1; l >= 0; l--) {
		if(pai[l][a] != pai[l][b])
			a = pai[l][a], b = pai[l][b];
	}
	return pai[0][a];
}

long long dist(int a, int b) { return depth[a] + depth[b] - 2*depth[LCA(a, b)]; }

vector<pair<int, long long>> vt[maxn];

void get_vt(vector<int>& v) {
	sort(v.begin(), v.end(), [](int a, int b) { return in[a] < in[b]; });
	
	int tam = v.size();
	for(int i = 1; i < tam; i++)
		v.push_back(LCA(v[i-1], v[i]));

	sort(v.begin(), v.end(), [](int a, int b) { return in[a] < in[b]; });
	v.erase(unique(v.begin(), v.end()), v.end());
	
	for(int x : v)
		vt[x].clear();

	for(int i = 1; i < v.size(); i++) {
		int lca = LCA(v[i-1], v[i]);
		long long DIST = depth[v[i]] - depth[lca];
		vt[lca].push_back({v[i], DIST});
		vt[v[i]].push_back({lca, DIST});
	}
}

void Init(int32_t N, int32_t A[], int32_t B[], int32_t D[]) {
	n = N;
	for(int i = 0; i < n-1; i++) {
		A[i]++, B[i]++;
		g[A[i]].push_back({B[i], D[i]}), g[B[i]].push_back({A[i], D[i]});
	}
	profundidade[1] = 1;
	depth[1] = 1;
	dfs(1);
	build_binary_lifting();
}

long long dd[maxn];
bool mark[maxn];

long long dijkstra(const vector<int>& tudo, const vector<int>& X, const vector<int>& Y) {
	static priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> q;
	for(int x : tudo)
		dd[x] = inf, mark[x] = 0;
	for(int x : Y)
		dd[x] = 0, q.push({0, x});

	while(q.size()) {
		int u = q.top().second;
		q.pop();
		if(mark[u]) continue;
		mark[u] = 1;

		for(auto [v, w] : vt[u])
			if(dd[v] > dd[u] + w)
				dd[v] = dd[u] + w, q.push({dd[v], v});
	}

	long long ans = inf;
	for(int x : X)
		ans = min(ans, dd[x]);
	return ans;
}

long long Query(int32_t S, int32_t X[], int32_t T, int32_t Y[]) {
	vector<int> v, x, y;
	for(int i = 0; i < S; i++)
		v.push_back(X[i]+1), x.push_back(X[i]+1);
	for(int i = 0; i < T; i++)
		v.push_back(Y[i]+1), y.push_back(Y[i]+1);
	get_vt(v);
	return dijkstra(v, x, y);
}

Compilation message

factories.cpp: In function 'void get_vt(std::vector<int>&)':
factories.cpp:59:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |  for(int i = 1; i < v.size(); i++) {
      |                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 55 ms 63288 KB Output is correct
2 Correct 1187 ms 72100 KB Output is correct
3 Correct 1339 ms 71992 KB Output is correct
4 Correct 1196 ms 72256 KB Output is correct
5 Correct 992 ms 72240 KB Output is correct
6 Correct 853 ms 71968 KB Output is correct
7 Correct 1347 ms 71876 KB Output is correct
8 Correct 1181 ms 72160 KB Output is correct
9 Correct 947 ms 72264 KB Output is correct
10 Correct 862 ms 71880 KB Output is correct
11 Correct 1298 ms 71928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 63044 KB Output is correct
2 Correct 1911 ms 129708 KB Output is correct
3 Correct 3249 ms 133252 KB Output is correct
4 Correct 1237 ms 127336 KB Output is correct
5 Correct 2149 ms 162184 KB Output is correct
6 Correct 3302 ms 134564 KB Output is correct
7 Correct 3103 ms 85688 KB Output is correct
8 Correct 1398 ms 98940 KB Output is correct
9 Correct 1778 ms 104720 KB Output is correct
10 Correct 2988 ms 100968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 63288 KB Output is correct
2 Correct 1187 ms 72100 KB Output is correct
3 Correct 1339 ms 71992 KB Output is correct
4 Correct 1196 ms 72256 KB Output is correct
5 Correct 992 ms 72240 KB Output is correct
6 Correct 853 ms 71968 KB Output is correct
7 Correct 1347 ms 71876 KB Output is correct
8 Correct 1181 ms 72160 KB Output is correct
9 Correct 947 ms 72264 KB Output is correct
10 Correct 862 ms 71880 KB Output is correct
11 Correct 1298 ms 71928 KB Output is correct
12 Correct 39 ms 63044 KB Output is correct
13 Correct 1911 ms 129708 KB Output is correct
14 Correct 3249 ms 133252 KB Output is correct
15 Correct 1237 ms 127336 KB Output is correct
16 Correct 2149 ms 162184 KB Output is correct
17 Correct 3302 ms 134564 KB Output is correct
18 Correct 3103 ms 85688 KB Output is correct
19 Correct 1398 ms 98940 KB Output is correct
20 Correct 1778 ms 104720 KB Output is correct
21 Correct 2988 ms 100968 KB Output is correct
22 Correct 4827 ms 165516 KB Output is correct
23 Correct 4213 ms 165512 KB Output is correct
24 Correct 5188 ms 169744 KB Output is correct
25 Correct 5054 ms 172504 KB Output is correct
26 Correct 6370 ms 162780 KB Output is correct
27 Correct 3563 ms 190328 KB Output is correct
28 Correct 2695 ms 160868 KB Output is correct
29 Correct 6073 ms 161704 KB Output is correct
30 Correct 6310 ms 161092 KB Output is correct
31 Correct 7199 ms 161484 KB Output is correct
32 Correct 2017 ms 107888 KB Output is correct
33 Correct 2139 ms 104720 KB Output is correct
34 Correct 3310 ms 98508 KB Output is correct
35 Correct 3000 ms 98260 KB Output is correct
36 Correct 3552 ms 98988 KB Output is correct
37 Correct 3061 ms 98924 KB Output is correct