Submission #960134

# Submission time Handle Problem Language Result Execution time Memory
960134 2024-04-09T17:56:12 Z aykhn Factories (JOI14_factories) C++17
100 / 100
3952 ms 235528 KB
#include <bits/stdc++.h>
#include "factories.h"
 
using namespace std;
 
#define inf 0x3F3F3F3F3F3F3F3F
 
const int MXN = 5e5 + 5;
const int LOG = 21;
 
int n;
vector<array<long long, 2>> adj[MXN];
int clo[LOG][MXN], sz[MXN], used[MXN];
long long dclo[LOG][MXN], dcent[MXN];

void getsize(int a, int p)
{
	sz[a] = 1;
	for (array<long long, 2> &v : adj[a])
	{
		if (used[v[0]] || v[0] == p) continue;
		getsize(v[0], a);
		sz[a] += sz[v[0]];
	}
}

int findcent(int a, int p, int &curn)
{
	for (array<long long, 2> &v : adj[a])
	{
		if (used[v[0]] || v[0] == p) continue;
		if (sz[v[0]] * 2 > curn) return findcent(v[0], a, curn);
	}
	return a;
}

void dfs(int a, int p, long long w, int &cc, int &lev)
{
	clo[lev][a] = cc;
	dclo[lev][a] = w;
	for (array<long long, 2> &v : adj[a])
	{
		if (used[v[0]] || v[0] == p) continue;
		dfs(v[0], a, w + v[1], cc, lev);
	}
}

void maincent(int a, int lev)
{
	getsize(a, a);
	int c = findcent(a, a, sz[a]);
	dfs(c, c, 0, c, lev);
	used[c] = 1;
	for (array<long long, 2> &v : adj[c])
	{
		if (used[v[0]]) continue;
		maincent(v[0], lev + 1);
	}
}
 
void Init(int N, int A[], int B[], int D[]) 
{
	n = N;
	for (int i = 0; i < n; i++)
	{
		adj[i].clear();
		sz[i] = 0;
		dcent[i] = -1;
		for (int j = 0; j < LOG; j++) clo[j][i] = -1;
	}
	for (int i = 0; i < n - 1; i++)
	{
		adj[A[i]].push_back({B[i], D[i]});
		adj[B[i]].push_back({A[i], D[i]});
	}
	maincent(0, 0);
}

 
long long Query(int S, int X[], int T, int Y[]) 
{
	long long res = inf;
	for (int i = 0; i < S; i++)
	{
		int x = X[i];
		for (int j = 0; j < LOG; j++)
		{
			if (clo[j][x] == -1) continue;
			if (dcent[clo[j][x]] == -1) dcent[clo[j][x]] = dclo[j][x];
			else dcent[clo[j][x]] = min(dcent[clo[j][x]], dclo[j][x]);
		}
	}
	for (int i = 0; i < T; i++)
	{
		int y = Y[i];
		for (int j = 0; j < LOG; j++)
		{
			if (clo[j][y] == -1 || dcent[clo[j][y]] == -1) continue;
			res = min(res, dclo[j][y] + dcent[clo[j][y]]);
		}
	}
	for (int i = 0; i < S; i++)
	{
		int x = X[i];
		for (int j = 0; j < LOG; j++)
		{
			if (clo[j][x] == -1) continue;
			dcent[clo[j][x]] = -1;
		}
	}
	return res;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 41564 KB Output is correct
2 Correct 268 ms 44888 KB Output is correct
3 Correct 272 ms 42836 KB Output is correct
4 Correct 274 ms 52564 KB Output is correct
5 Correct 280 ms 50768 KB Output is correct
6 Correct 222 ms 51792 KB Output is correct
7 Correct 283 ms 52356 KB Output is correct
8 Correct 279 ms 50256 KB Output is correct
9 Correct 273 ms 46672 KB Output is correct
10 Correct 208 ms 47696 KB Output is correct
11 Correct 270 ms 48308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 37720 KB Output is correct
2 Correct 1628 ms 167144 KB Output is correct
3 Correct 2571 ms 187400 KB Output is correct
4 Correct 720 ms 131716 KB Output is correct
5 Correct 3397 ms 235528 KB Output is correct
6 Correct 2481 ms 206056 KB Output is correct
7 Correct 922 ms 80284 KB Output is correct
8 Correct 378 ms 67292 KB Output is correct
9 Correct 951 ms 84432 KB Output is correct
10 Correct 837 ms 80800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 41564 KB Output is correct
2 Correct 268 ms 44888 KB Output is correct
3 Correct 272 ms 42836 KB Output is correct
4 Correct 274 ms 52564 KB Output is correct
5 Correct 280 ms 50768 KB Output is correct
6 Correct 222 ms 51792 KB Output is correct
7 Correct 283 ms 52356 KB Output is correct
8 Correct 279 ms 50256 KB Output is correct
9 Correct 273 ms 46672 KB Output is correct
10 Correct 208 ms 47696 KB Output is correct
11 Correct 270 ms 48308 KB Output is correct
12 Correct 7 ms 37720 KB Output is correct
13 Correct 1628 ms 167144 KB Output is correct
14 Correct 2571 ms 187400 KB Output is correct
15 Correct 720 ms 131716 KB Output is correct
16 Correct 3397 ms 235528 KB Output is correct
17 Correct 2481 ms 206056 KB Output is correct
18 Correct 922 ms 80284 KB Output is correct
19 Correct 378 ms 67292 KB Output is correct
20 Correct 951 ms 84432 KB Output is correct
21 Correct 837 ms 80800 KB Output is correct
22 Correct 2331 ms 189472 KB Output is correct
23 Correct 2159 ms 193000 KB Output is correct
24 Correct 3237 ms 210292 KB Output is correct
25 Correct 3362 ms 212856 KB Output is correct
26 Correct 3256 ms 210652 KB Output is correct
27 Correct 3952 ms 234328 KB Output is correct
28 Correct 1222 ms 138364 KB Output is correct
29 Correct 3278 ms 209544 KB Output is correct
30 Correct 3370 ms 209304 KB Output is correct
31 Correct 3297 ms 210120 KB Output is correct
32 Correct 1070 ms 124532 KB Output is correct
33 Correct 383 ms 106804 KB Output is correct
34 Correct 737 ms 116180 KB Output is correct
35 Correct 644 ms 112972 KB Output is correct
36 Correct 840 ms 116572 KB Output is correct
37 Correct 895 ms 118368 KB Output is correct