# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
269825 |
2020-08-17T10:35:37 Z |
shivensinha4 |
Race (IOI11_race) |
C++17 |
|
2290 ms |
164532 KB |
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
#define SSTR(x) static_cast<std::ostringstream&>((std::ostringstream() << std::dec << x)).str()
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
//#define endl '\n'
int n, k, l, timer = 0;
vector<vector<pair<int, ll>>> adjList;
vi removed, subtreeSize, centroid, tin, tout;
vector<unordered_map<ll, vector<ii>>> paths; // {length, {child, roads}}
vector<vi> up; vector<vector<ll>> upDist;
vector<ll> depth, edges;
int ans = 1e9;
int dfs(int p, int parent, ll d) {
int s = 1;
tin[p] = ++timer;
depth[p] = d;
edges[p] = edges[parent] + 1;
for_(i, 1, l+1) up[p][i] = up[up[p][i-1]][i-1];
for (auto i: adjList[p]) if (i.first != parent) {
up[i.first][0] = p;
s += dfs(i.first, p, d+i.second);
}
subtreeSize[p] = s;
tout[p] = ++timer;
return s;
}
bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v) {
if (is_ancestor(u, v)) return u;
if (is_ancestor(v, u)) return v;
for (int i = l; i >= 0; --i) if (!is_ancestor(up[u][i], v)) u = up[u][i];
return up[u][0];
}
// {distance, edges}
//pair<ll, int> ancDist(int u, int anc) {
//if (u == anc) return {0, 0};
//ll dist = 0, edges = 0;
//for (int i = l; i >= 0; --i) if (!is_ancestor(up[u][i], anc)) {
//dist += upDist[u][i];
//edges += (1 << i);
//u = up[u][i];
//}
//return {dist + upDist[u][0], edges+1};
//}
pair<ll, int> dist(int u, int v) {
int lc = lca(u, v);
return {depth[u] + depth[v] - 2*depth[lc], edges[u] + edges[v] - 2*edges[lc]};
}
void decompose(int p, int c) {
int invalidChild = -1, sizeLimit = (subtreeSize[p] >> 1);
for (auto i: adjList[p]) if (!removed[i.first] and subtreeSize[i.first] > sizeLimit) {
invalidChild = i.first;
break;
}
if (invalidChild != -1) {
subtreeSize[p] -= subtreeSize[invalidChild];
subtreeSize[invalidChild] += subtreeSize[p];
return decompose(invalidChild, c);
}
removed[p] = true;
centroid[p] = c;
for (auto i: adjList[p]) if (!removed[i.first]) {
centroid[i.first] = p;
decompose(i.first, p);
}
}
void updateParents(int p) {
int a = p, b = centroid[p];
while (b != -1) {
ii d = dist(p, b);
paths[b][d.first].push_back({d.second, a});
a = b; b = centroid[b];
}
}
void solve(int p) {
int a = p, b = centroid[p];
while (b != -1) {
ii d = dist(p, b);
for (auto o: paths[b][k-d.first]) if (o.second != a) {
ans = min(ans, o.first + d.second);
break;
}
a = b; b = centroid[b];
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N, k = K;
adjList.resize(n); subtreeSize.resize(n); removed.resize(n); paths.resize(n); centroid.resize(n, -1); tin.resize(n); tout.resize(n); up.resize(n); depth.resize(n); edges.resize(n);
l = ceil(log2(n));
up.assign(n, vi(l+1)); upDist.assign(n, vector<ll>(l+1));
for_(i, 0, n-1) {
int a, b; ll w; a = H[i][0], b = H[i][1]; w = L[i];
adjList[a].push_back({b, w});
adjList[b].push_back({a, w});
}
dfs(0, 0, 0);
decompose(0, -1);
for_(i, 0, n) updateParents(i);
for_(i, 0, n) {
paths[i][0].push_back({0, -1});
for (auto o: paths[i]) if (o.second.size() > 1) {
sort(paths[i][o.first].begin(), paths[i][o.first].begin()+2);
}
}
for_(i, 0, n) solve(i);
return (ans == 1e9 ? -1 : ans);
}
//int main() {
//#ifndef ONLINE_JUDGE
//freopen("test.in", "r", stdin);
//#endif
//ios_base::sync_with_stdio(false);
//cin.tie(0);
//int H[100000][2], L[100000];
//int N, K;
//cin >> N >> K;
//for_(i, 0, N-1) {
//int a, b; ll w; cin >> a >> b >> w;
//H[i][0] = a; H[i][1] = b;
//L[i] = w;
//}
//cout << best_path(N, K, H, L) << endl;
//return 0;
//}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
1280 KB |
Output is correct |
22 |
Correct |
5 ms |
1664 KB |
Output is correct |
23 |
Correct |
5 ms |
1664 KB |
Output is correct |
24 |
Correct |
5 ms |
1536 KB |
Output is correct |
25 |
Correct |
5 ms |
1536 KB |
Output is correct |
26 |
Correct |
4 ms |
1408 KB |
Output is correct |
27 |
Correct |
4 ms |
1024 KB |
Output is correct |
28 |
Correct |
5 ms |
1536 KB |
Output is correct |
29 |
Correct |
4 ms |
1536 KB |
Output is correct |
30 |
Correct |
5 ms |
1536 KB |
Output is correct |
31 |
Correct |
6 ms |
1536 KB |
Output is correct |
32 |
Correct |
5 ms |
1536 KB |
Output is correct |
33 |
Correct |
5 ms |
1764 KB |
Output is correct |
34 |
Correct |
6 ms |
1792 KB |
Output is correct |
35 |
Correct |
5 ms |
1664 KB |
Output is correct |
36 |
Correct |
5 ms |
1664 KB |
Output is correct |
37 |
Correct |
7 ms |
1536 KB |
Output is correct |
38 |
Correct |
7 ms |
1664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1264 ms |
103120 KB |
Output is correct |
20 |
Correct |
1292 ms |
102904 KB |
Output is correct |
21 |
Correct |
1129 ms |
100976 KB |
Output is correct |
22 |
Correct |
1073 ms |
96632 KB |
Output is correct |
23 |
Correct |
2290 ms |
164532 KB |
Output is correct |
24 |
Correct |
804 ms |
91512 KB |
Output is correct |
25 |
Incorrect |
1533 ms |
91936 KB |
Output isn't correct |
26 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
1280 KB |
Output is correct |
22 |
Correct |
5 ms |
1664 KB |
Output is correct |
23 |
Correct |
5 ms |
1664 KB |
Output is correct |
24 |
Correct |
5 ms |
1536 KB |
Output is correct |
25 |
Correct |
5 ms |
1536 KB |
Output is correct |
26 |
Correct |
4 ms |
1408 KB |
Output is correct |
27 |
Correct |
4 ms |
1024 KB |
Output is correct |
28 |
Correct |
5 ms |
1536 KB |
Output is correct |
29 |
Correct |
4 ms |
1536 KB |
Output is correct |
30 |
Correct |
5 ms |
1536 KB |
Output is correct |
31 |
Correct |
6 ms |
1536 KB |
Output is correct |
32 |
Correct |
5 ms |
1536 KB |
Output is correct |
33 |
Correct |
5 ms |
1764 KB |
Output is correct |
34 |
Correct |
6 ms |
1792 KB |
Output is correct |
35 |
Correct |
5 ms |
1664 KB |
Output is correct |
36 |
Correct |
5 ms |
1664 KB |
Output is correct |
37 |
Correct |
7 ms |
1536 KB |
Output is correct |
38 |
Correct |
7 ms |
1664 KB |
Output is correct |
39 |
Correct |
1264 ms |
103120 KB |
Output is correct |
40 |
Correct |
1292 ms |
102904 KB |
Output is correct |
41 |
Correct |
1129 ms |
100976 KB |
Output is correct |
42 |
Correct |
1073 ms |
96632 KB |
Output is correct |
43 |
Correct |
2290 ms |
164532 KB |
Output is correct |
44 |
Correct |
804 ms |
91512 KB |
Output is correct |
45 |
Incorrect |
1533 ms |
91936 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |