Submission #496197

# Submission time Handle Problem Language Result Execution time Memory
496197 2021-12-21T02:02:36 Z luanaamorim Race (IOI11_race) C++17
100 / 100
580 ms 32592 KB
#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring>
#include <set>
#include <stack>
#include <bitset>
#include "race.h"
#define ll long long
#define INF (1e9)
#define MAX (int) (2e5 + 5)
#define MOD 1000000007
#define par pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(x) (int) ((x).size())
#define esq(x) (x<<1)
#define dir(x) ((x<<1)|1)
#define lsb(x) (x & -x)
#define W(x) cout << #x << ": " << x << endl
#define Wii(x) cout << x.first << ' ' << x.second << endl
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int n, k, sub[MAX], vis[MAX], h[MAX][2], l[MAX];
vector<par> grafo[MAX];
par f[MAX*10];

int dfs(int u, int p = 0)
{
	if (vis[u]) return 0;
	sub[u] = 1;
	for (auto[v, w] : grafo[u])
		if (v!=p) sub[u] += dfs(v, u);
	return sub[u];
}

int centroid(int u, int p, int t)
{	
	for (auto[v, w] : grafo[u])
		if (v!=p && !vis[v] && sub[v]>t/2) return centroid(v, u, t);
	return u; 
}

int query(int u, int p, int h, int d)
{	
	if (vis[u] || d>k) return INF;
	int resp = INF;
	if (f[k-d].first) resp = f[k-d].second + h;

	for (auto[v, w] : grafo[u])
		if (v!=p) resp = min(resp, query(v, u, h+1, d+w));
	return resp;
}

void update(int u, int p, int h, int d)
{
	if (vis[u] || d>k) return;
	if (f[d].first) f[d].second = min(f[d].second, h);
	else f[d].second = h;
	f[d].first=1;

	for (auto[v, w] : grafo[u])
		if (v!=p) update(v, u, h+1, d+w);
}

void ini(int u, int p, int d)
{
	if (vis[u] || d>k) return;
	f[d] = {0, 0};

	for (auto[v, w] : grafo[u])
		if (v!=p) ini(v, u, d+w);
}

int solve(int u)
{
	dfs(u);
	int c = centroid(u, 0, sub[u]);
	vis[c] = 1;
	int resp = INF;

	f[0] = {1, 0};
	for (auto[v, w] : grafo[c])
	{
		resp = min(resp, query(v, 0, 1, w));
 		update(v, 0, 1, w);
	}	

	f[0] = {0, 0};
	for (auto[v, w] : grafo[c])
		ini(v, 0, w);

	for (auto[v, w] : grafo[c])
		if (!vis[v]) resp = min(resp, solve(v));

	return resp;
}

int best_path(int N, int K, int H[MAX][2], int L[MAX])
{
	k = K;
	for (int i = 0; i < N-1; i++)
	{
		grafo[H[i][0]+1].push_back({H[i][1]+1, L[i]});
		grafo[H[i][1]+1].push_back({H[i][0]+1, L[i]});
	}

	int resp = solve(1);
	return (resp==INF ? -1 : resp);
}

// int main()
// {
// 	cin >> n >> k;
// 	for (int i = 0; i < n-1; i++)
// 	{
// 		cin >> h[i][0] >> h[i][1] >> l[i];
// 	}

// 	cout << best_path(n, k, h, l) << endl;
// }


















# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 2 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4984 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 2 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4984 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 8 ms 8352 KB Output is correct
23 Correct 6 ms 7884 KB Output is correct
24 Correct 8 ms 9164 KB Output is correct
25 Correct 8 ms 8140 KB Output is correct
26 Correct 5 ms 7372 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 6636 KB Output is correct
29 Correct 5 ms 7500 KB Output is correct
30 Correct 8 ms 7756 KB Output is correct
31 Correct 5 ms 8396 KB Output is correct
32 Correct 5 ms 8368 KB Output is correct
33 Correct 6 ms 9804 KB Output is correct
34 Correct 6 ms 9012 KB Output is correct
35 Correct 7 ms 10060 KB Output is correct
36 Correct 6 ms 10316 KB Output is correct
37 Correct 6 ms 8268 KB Output is correct
38 Correct 5 ms 7244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 2 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4984 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 161 ms 10628 KB Output is correct
20 Correct 123 ms 10692 KB Output is correct
21 Correct 178 ms 10652 KB Output is correct
22 Correct 154 ms 10656 KB Output is correct
23 Correct 75 ms 10880 KB Output is correct
24 Correct 56 ms 10768 KB Output is correct
25 Correct 123 ms 13416 KB Output is correct
26 Correct 97 ms 16264 KB Output is correct
27 Correct 161 ms 16444 KB Output is correct
28 Correct 247 ms 27584 KB Output is correct
29 Correct 305 ms 26728 KB Output is correct
30 Correct 183 ms 16452 KB Output is correct
31 Correct 188 ms 16460 KB Output is correct
32 Correct 244 ms 16468 KB Output is correct
33 Correct 249 ms 15320 KB Output is correct
34 Correct 164 ms 15288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 2 ms 4940 KB Output is correct
14 Correct 2 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4984 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 4 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 8 ms 8352 KB Output is correct
23 Correct 6 ms 7884 KB Output is correct
24 Correct 8 ms 9164 KB Output is correct
25 Correct 8 ms 8140 KB Output is correct
26 Correct 5 ms 7372 KB Output is correct
27 Correct 3 ms 5068 KB Output is correct
28 Correct 4 ms 6636 KB Output is correct
29 Correct 5 ms 7500 KB Output is correct
30 Correct 8 ms 7756 KB Output is correct
31 Correct 5 ms 8396 KB Output is correct
32 Correct 5 ms 8368 KB Output is correct
33 Correct 6 ms 9804 KB Output is correct
34 Correct 6 ms 9012 KB Output is correct
35 Correct 7 ms 10060 KB Output is correct
36 Correct 6 ms 10316 KB Output is correct
37 Correct 6 ms 8268 KB Output is correct
38 Correct 5 ms 7244 KB Output is correct
39 Correct 161 ms 10628 KB Output is correct
40 Correct 123 ms 10692 KB Output is correct
41 Correct 178 ms 10652 KB Output is correct
42 Correct 154 ms 10656 KB Output is correct
43 Correct 75 ms 10880 KB Output is correct
44 Correct 56 ms 10768 KB Output is correct
45 Correct 123 ms 13416 KB Output is correct
46 Correct 97 ms 16264 KB Output is correct
47 Correct 161 ms 16444 KB Output is correct
48 Correct 247 ms 27584 KB Output is correct
49 Correct 305 ms 26728 KB Output is correct
50 Correct 183 ms 16452 KB Output is correct
51 Correct 188 ms 16460 KB Output is correct
52 Correct 244 ms 16468 KB Output is correct
53 Correct 249 ms 15320 KB Output is correct
54 Correct 164 ms 15288 KB Output is correct
55 Correct 11 ms 5544 KB Output is correct
56 Correct 13 ms 5580 KB Output is correct
57 Correct 80 ms 10840 KB Output is correct
58 Correct 33 ms 10864 KB Output is correct
59 Correct 97 ms 17084 KB Output is correct
60 Correct 580 ms 31904 KB Output is correct
61 Correct 171 ms 16436 KB Output is correct
62 Correct 234 ms 16492 KB Output is correct
63 Correct 278 ms 16680 KB Output is correct
64 Correct 547 ms 18488 KB Output is correct
65 Correct 219 ms 16324 KB Output is correct
66 Correct 444 ms 32592 KB Output is correct
67 Correct 102 ms 16688 KB Output is correct
68 Correct 271 ms 22084 KB Output is correct
69 Correct 273 ms 25608 KB Output is correct
70 Correct 263 ms 24836 KB Output is correct