Submission #154931

# Submission time Handle Problem Language Result Execution time Memory
154931 2019-09-25T13:54:02 Z pamaj Factories (JOI14_factories) C++17
100 / 100
5120 ms 169928 KB
#include <bits/stdc++.h>
#include "factories.h"

#define ff first
#define ss second

using namespace std;

const int maxn = 5e5+10;
const long long inf = 1e18+10;

typedef pair<int, int> pii;
typedef long long ll;

int n;
int sz[maxn], pai[maxn], nivel[maxn];

ll dist[maxn][19], child[maxn];

bool mark[maxn];

vector<pii> grafo[maxn];

int dfs(int u, int p)
{
	sz[u] = 1;
	for (auto v: grafo[u])
		if (v.first != p && !mark[v.first])
			sz[u] += dfs(v.first, u);

	return sz[u];
}

int centroid(int S, int u, int p)
{
	for (auto v: grafo[u])
		if (v.first != p && !mark[v.first] && sz[v.first] > S/2)
			return centroid(S, v.first, u);

	return u;
}

void upd(int u, int p, int lvl, ll d)
{	
	dist[u][lvl] = d;

	for (auto v: grafo[u])
		if (v.first != p && !mark[v.first])
			upd(v.first, u, lvl, d+(ll)v.second);
}

void decompose(int u, int p, int lvl)
{
	dfs(u, -1);

	int c = centroid(sz[u], u, -1);
	mark[c] = 1, nivel[c] = lvl;

	upd(c, -1, lvl, 0LL);

	pai[c] = p;

	for (auto v: grafo[c])
		if (!mark[v.first])
			decompose(v.first, c, lvl+1);
}

void Init(int N, int A[], int B[], int D[])
{
	n = N;

	for (int i = 0; i < n-1; i++)
	{
		grafo[A[i]].push_back({B[i], D[i]});
		grafo[B[i]].push_back({A[i], D[i]});
	}

	for (int i = 0; i < n; i++)
		child[i] = inf;

	decompose(0, -1, 0);
}

long long Query(int S, int X[], int T, int Y[])
{
	int s = S, t = T;

	for (int i = 0; i < t; i++)
	{
		int v = Y[i];
		
		while (v != -1)
		{
			child[v] = min(child[v], dist[Y[i]][nivel[v]]);

			v = pai[v];
		}
	}

	ll ans = inf;

	for (int i = 0; i < s; i++)
	{
		int v = X[i];

		while (v != -1)
		{
			ans = min(ans, dist[X[i]][nivel[v]]+child[v]);

			v = pai[v];
		}
	}

	for (int i = 0; i < t; i++)
	{
		int v = Y[i];
		
		while (v != -1)
		{
			child[v] = inf;

			v = pai[v];
		}
	}

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 368 ms 30712 KB Output is correct
3 Correct 401 ms 30680 KB Output is correct
4 Correct 397 ms 30824 KB Output is correct
5 Correct 417 ms 30840 KB Output is correct
6 Correct 286 ms 30648 KB Output is correct
7 Correct 396 ms 30692 KB Output is correct
8 Correct 403 ms 30724 KB Output is correct
9 Correct 416 ms 30840 KB Output is correct
10 Correct 291 ms 30840 KB Output is correct
11 Correct 398 ms 30684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 12280 KB Output is correct
2 Correct 2110 ms 146896 KB Output is correct
3 Correct 3325 ms 146912 KB Output is correct
4 Correct 1008 ms 147380 KB Output is correct
5 Correct 4288 ms 164324 KB Output is correct
6 Correct 3402 ms 149016 KB Output is correct
7 Correct 1108 ms 57388 KB Output is correct
8 Correct 496 ms 58468 KB Output is correct
9 Correct 1310 ms 61192 KB Output is correct
10 Correct 1159 ms 58948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 12664 KB Output is correct
2 Correct 368 ms 30712 KB Output is correct
3 Correct 401 ms 30680 KB Output is correct
4 Correct 397 ms 30824 KB Output is correct
5 Correct 417 ms 30840 KB Output is correct
6 Correct 286 ms 30648 KB Output is correct
7 Correct 396 ms 30692 KB Output is correct
8 Correct 403 ms 30724 KB Output is correct
9 Correct 416 ms 30840 KB Output is correct
10 Correct 291 ms 30840 KB Output is correct
11 Correct 398 ms 30684 KB Output is correct
12 Correct 15 ms 12280 KB Output is correct
13 Correct 2110 ms 146896 KB Output is correct
14 Correct 3325 ms 146912 KB Output is correct
15 Correct 1008 ms 147380 KB Output is correct
16 Correct 4288 ms 164324 KB Output is correct
17 Correct 3402 ms 149016 KB Output is correct
18 Correct 1108 ms 57388 KB Output is correct
19 Correct 496 ms 58468 KB Output is correct
20 Correct 1310 ms 61192 KB Output is correct
21 Correct 1159 ms 58948 KB Output is correct
22 Correct 2563 ms 154128 KB Output is correct
23 Correct 2632 ms 156820 KB Output is correct
24 Correct 4067 ms 155420 KB Output is correct
25 Correct 4041 ms 158836 KB Output is correct
26 Correct 4051 ms 155372 KB Output is correct
27 Correct 5120 ms 169928 KB Output is correct
28 Correct 1245 ms 157700 KB Output is correct
29 Correct 4137 ms 155124 KB Output is correct
30 Correct 4491 ms 155072 KB Output is correct
31 Correct 4211 ms 155224 KB Output is correct
32 Correct 1379 ms 61432 KB Output is correct
33 Correct 521 ms 58868 KB Output is correct
34 Correct 896 ms 55564 KB Output is correct
35 Correct 883 ms 55456 KB Output is correct
36 Correct 1149 ms 55824 KB Output is correct
37 Correct 1166 ms 55928 KB Output is correct