QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#145354 | #2002. Race | K8He | 0 | 18ms | 19596kb | C++14 | 2.8kb | 2023-08-22 09:19:53 | 2023-08-22 09:19:55 |
Judging History
answer
#include <bits/stdc++.h>
#include "race.h"
#define lowb(a, r, x) lower_bound(a + 1, a + r + 1, x) - a
#define uppb(a, r, x) upper_bound(a + 1, a + r + 1, x) - a
#define _for(i, a, b) for (ll i = a; i <= b; ++i)
#define for_(i, a, b) for (ll i = a; i >= b; --i)
#define far(i, vec) for (auto i : vec)
#define bdmd ll mid = (l + r) >> 1
#define NO nullptr
typedef long double ldb;
typedef long long ll;
typedef double db;
typedef std::pair <ll, ll> pll;
const ll N = 2e5 + 10, K = 1e6 + 10, inf = 1ll << 60;
namespace SOLVE {
ll n, k, root, sz[N], mx[N], len[K], ans;
std::vector <pll> tu[N], tmp;
bool vis[N];
inline ll rnt () {
ll x = 0, w = 1; char c = getchar ();
while (!isdigit (c)) { if (c == '-') w = -1; c = getchar (); }
while (isdigit (c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar ();
return x * w;
}
void GetDis (ll u, ll fa, ll s, ll d) {
if (s > k) return;
tmp.emplace_back (s, d);
far (p, tu[u]) {
ll v = p.first, w = p.second;
if (vis[v] || v == fa) continue;
GetDis (v, u, s + w, d + 1);
}
return;
}
void Clear (ll u, ll fa, ll s, ll d) {
if (s > k) return;
len[s] = inf;
far (p, tu[u]) {
ll v = p.first, w = p.second;
if (vis[v] || v == fa) continue;
Clear (v, u, s + w, d + 1);
}
return;
}
inline void Calc (ll u) {
len[0] = 0;
far (p, tu[u]) {
ll v = p.first, w = p.second;
if (vis[v]) continue;
GetDis (v, u, w, 1);
far (j, tmp) {
ll t = k - j.first;
if (len[t] < inf) ans = std::min (ans, len[t] + j.second);
}
far (j, tmp) len[j.first] = std::min (len[j.first], j.second);
std::vector <pll> ().swap (tmp);
}
Clear (u, 0, 0, 0);
return;
}
void GetRoot (ll u, ll fa) {
sz[u] = 1, mx[u] = 0;
far (p, tu[u]) {
ll v = p.first, w = p.second;
if (vis[v] || v == fa) continue;
GetRoot (v, u), sz[u] += sz[v];
mx[u] = std::max (mx[u], sz[v]);
}
mx[u] = std::max (mx[u], sz[0] - sz[u]);
if (mx[u] < mx[root]) root = u;
return;
}
void Divide (ll u) {
Calc (u);
vis[u] = true;
far (p, tu[u]) {
ll v = p.first, w = p.second;
if (vis[v]) continue;
sz[0] = sz[v], mx[root = 0] = inf;
GetRoot (v, u);
Divide (root);
}
return;
}
inline void In (int N, int K, int H[][2], int L[]) {
n = N, k = K;
_for (i, 1, n - 1) {
ll u = H[i][0] + 1, v = H[i][1] + 1, w = L[i];
tu[u].emplace_back (v, w);
tu[v].emplace_back (u, w);
}
return;
}
inline void Solve () {
ans = inf, sz[0] = n, mx[root = 0] = inf;
_for (i, 1, k) len[i] = inf;
GetRoot (1, 0);
Divide (root);
return;
}
inline void Out () {
printf ("%lld\n", ans == inf ? -1 : ans);
return;
}
}
int best_path(int N, int K, int H[][2], int L[]) {
SOLVE::In (N, K, H, L);
SOLVE::Solve ();
return SOLVE::ans == inf ? -1 : SOLVE::ans;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 18168kb
input:
100 50 0 1 1 1 2 2 2 3 2 3 4 1 4 5 2 5 6 1 6 7 1 7 8 1 8 9 1 9 10 2 10 11 2 11 12 2 12 13 1 13 14 1 14 15 1 15 16 2 16 17 1 17 18 2 18 19 1 19 20 1 20 21 1 21 22 2 22 23 2 23 24 2 24 25 2 25 26 1 26 27 2 27 28 2 28 29 2 29 30 2 30 31 2 31 32 1 32 33 1 33 34 2 34 35 2 35 36 1 36 37 1 37 38 1 38 39 1 ...
output:
Incorrect. Returned -1, Expected 30.
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #39:
score: 0
Wrong Answer
time: 18ms
memory: 19596kb
input:
100000 100 1 0 1 2 1 10 3 1 1 4 3 5 5 3 6 6 5 6 7 3 10 8 5 9 9 8 7 10 9 9 11 6 7 12 6 3 13 10 10 14 9 1 15 14 7 16 15 5 17 10 1 18 14 9 19 12 8 20 18 10 21 10 9 22 12 7 23 14 9 24 15 5 25 15 2 26 20 4 27 19 10 28 17 8 29 16 8 30 24 10 31 17 2 32 28 7 33 27 8 34 21 4 35 28 7 36 22 4 37 18 6 38 27 6 3...
output:
Incorrect. Returned -1, Expected 11.
Subtask #4:
score: 0
Skipped
Dependency #1:
0%