답안 #256897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
256897 2020-08-03T11:09:46 Z TeaTime 공장들 (JOI14_factories) C++17
100 / 100
5425 ms 361848 KB
#include "factories.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <tuple>
#include <random>
#include <cmath>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SZ = 5e5 + 10, INF = 1e9 * 1e9;
int sizes[SZ], x[SZ], y[SZ];
bool used[SZ];
vector<vector<pair<ll, ll>>> vec;

vector<vector<pair<ll, ll>>> gr;
ll curC = 0;
vector<ll> bst;

ll sz(int v, ll dist = 0, int par = -1) {
	ll s = 1;
	vec[v].push_back({ curC, dist });

	for (auto to : gr[v]) {
		if (!used[to.first] && to.first != par) {
			s += sz(to.first, dist + to.second, v);
		}
	}

	sizes[v] = s;
	return s;
}

ll cntr(int v, int c, int par = -1) {
	for (auto to : gr[v]) {
		if (to.first != par && !used[to.first] && sizes[to.first] >= c / 2) {
			return cntr(to.first, c, v);
		}
	}

	return v;
}

void decomp(int v) {
	used[v] = 1;
	curC = v;
	sz(v);

	for (auto to : gr[v]) {
		if (!used[to.first]) {
			decomp(cntr(to.first, sizes[to.first]));
		}
	}
}

void Init(int N, int A[], int B[], int D[]) {
	gr.clear();
	vec.clear();
	bst.clear();
	gr.resize(N);
	vec.resize(N);
	for (int i = 0; i < N - 1; i++) {
		gr[A[i]].push_back({ B[i], D[i] });
		gr[B[i]].push_back({ A[i], D[i] });
	}

	bst.resize(N);
	for (auto &c : bst) c = INF;
	decomp(0);
}

long long Query(int S, int X[], int T, int Y[]) {
	for (int i = 0; i < S; i++) {
		ll ind = X[i];
		for (auto cur : vec[ind]) {
			bst[cur.first] = min(bst[cur.first], cur.second);
		}
	}

	ll ans = INF;
	for (int i = 0; i < T; i++) {
		ll ind = Y[i];
		for (auto cur : vec[ind]) {
			ans = min(ans, bst[cur.first] + cur.second);
		}
	}
	for (int i = 0; i < S; i++) {
		ll ind = X[i];
		for (auto cur : vec[ind]) {
			bst[cur.first] = INF;
		}
	}
	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 768 KB Output is correct
2 Correct 464 ms 9948 KB Output is correct
3 Correct 473 ms 10404 KB Output is correct
4 Correct 486 ms 10492 KB Output is correct
5 Correct 504 ms 10872 KB Output is correct
6 Correct 372 ms 9336 KB Output is correct
7 Correct 475 ms 10360 KB Output is correct
8 Correct 480 ms 10360 KB Output is correct
9 Correct 492 ms 10788 KB Output is correct
10 Correct 379 ms 9532 KB Output is correct
11 Correct 477 ms 10232 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 2810 ms 201864 KB Output is correct
3 Correct 4153 ms 300464 KB Output is correct
4 Correct 1478 ms 105476 KB Output is correct
5 Correct 5425 ms 361848 KB Output is correct
6 Correct 4585 ms 300892 KB Output is correct
7 Correct 1588 ms 54776 KB Output is correct
8 Correct 800 ms 30536 KB Output is correct
9 Correct 1710 ms 64972 KB Output is correct
10 Correct 1454 ms 55816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 768 KB Output is correct
2 Correct 464 ms 9948 KB Output is correct
3 Correct 473 ms 10404 KB Output is correct
4 Correct 486 ms 10492 KB Output is correct
5 Correct 504 ms 10872 KB Output is correct
6 Correct 372 ms 9336 KB Output is correct
7 Correct 475 ms 10360 KB Output is correct
8 Correct 480 ms 10360 KB Output is correct
9 Correct 492 ms 10788 KB Output is correct
10 Correct 379 ms 9532 KB Output is correct
11 Correct 477 ms 10232 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 2810 ms 201864 KB Output is correct
14 Correct 4153 ms 300464 KB Output is correct
15 Correct 1478 ms 105476 KB Output is correct
16 Correct 5425 ms 361848 KB Output is correct
17 Correct 4585 ms 300892 KB Output is correct
18 Correct 1588 ms 54776 KB Output is correct
19 Correct 800 ms 30536 KB Output is correct
20 Correct 1710 ms 64972 KB Output is correct
21 Correct 1454 ms 55816 KB Output is correct
22 Correct 3229 ms 202356 KB Output is correct
23 Correct 3272 ms 206072 KB Output is correct
24 Correct 4686 ms 277332 KB Output is correct
25 Correct 4441 ms 297768 KB Output is correct
26 Correct 4422 ms 308336 KB Output is correct
27 Correct 5183 ms 361748 KB Output is correct
28 Correct 1720 ms 115672 KB Output is correct
29 Correct 4400 ms 307704 KB Output is correct
30 Correct 4400 ms 325024 KB Output is correct
31 Correct 4296 ms 325884 KB Output is correct
32 Correct 1581 ms 81044 KB Output is correct
33 Correct 963 ms 44776 KB Output is correct
34 Correct 1216 ms 59256 KB Output is correct
35 Correct 1118 ms 59512 KB Output is correct
36 Correct 1310 ms 66552 KB Output is correct
37 Correct 1439 ms 66908 KB Output is correct