QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#86120 | #5102. Dungeon Crawler | yunny_world | TL | 2ms | 7932kb | C++14 | 2.8kb | 2023-03-09 13:15:01 | 2023-03-09 13:15:04 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long int
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define X first
#define Y second
#define all(v) v.begin(),v.end()
using namespace std;
ll gcd(ll a, ll b)
{
if (b == 0) return a;
return gcd(b, a % b);
}
#define lcm(a, b) a * b / gcd(a, b)
const ll INF = 1e13;
const ll MOD = 1e6 + 7;
const double PI = 3.14159265359;
int dx[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int dy[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
ll n, q;
ll distSum;
ll ecnt;
ll e[2005][4005]; // the label of i-th visited node in the tour, e[root][i]
ll h[2005][4005];
ll p[2005][4005][13]; // p[root][i][j]
ll d[2005][2005]; // 깊이, d[root][i]
ll dist[2005][2005]; // 거리, dist[root][i]
vector<pair<ll, ll>> g[2005];
ll log(ll x) {
return 32 - __builtin_clz(x) - 1;
}
void dfs(ll r, ll now, ll par)
{
e[r][++ecnt] = now;
for (auto nxt : g[now])
if (nxt.X != par)
{
d[r][nxt.X] = d[r][now] + 1;
dist[r][nxt.X] = dist[r][now] + nxt.Y;
dfs(r, nxt.X, now);
}
e[r][++ecnt] = par;
}
void solve()
{
cin >> n >> q;
for (ll i = 0; i < n - 1; i++)
{
ll u, v, w; cin >> u >> v >> w;
g[u].push_back({ v, w });
g[v].push_back({ u, w });
distSum += w;
}
for (ll r = 1; r <= n; r++)
{
ecnt = 0;
dfs(r, r, 0);
for (ll i = 1; i < 2 * n; i++)
{
if (!h[r][e[r][i]]) h[r][e[r][i]] = i;
p[r][i][0] = e[r][i];
}
for (ll j = 1; (1 << j) < 2 * n; j++)
{
for (ll i = 1; i + (1 << j) - 1 < 2 * n; i++)
{
if (d[r][p[r][i][j - 1]] < d[r][p[r][i + (1 << (j - 1))][j - 1]])
p[r][i][j] = p[r][i][j - 1];
else
p[r][i][j] = p[r][i + (1 << (j - 1))][j - 1];
}
}
}
while (q--)
{
ll s, k, t; cin >> s >> k >> t;
ll kt, u = h[s][k], v = h[s][t];
if (u > v) swap(u, v);
ll j = log(v - u + 1);
if (d[s][p[s][u][j]] < d[s][p[s][v - (1 << j) + 1][j]])
kt = p[s][u][j];
else
kt = p[s][v - (1 << j) + 1][j];
if (kt == t) cout << "impossible\n";
else
{
ll ans = INF;
for (ll i = 1; i <= n; i++)
{
ll ki, u = h[s][k], v = h[s][i];
if (u > v) swap(u, v);
ll j = log(v - u + 1);
if (d[s][p[s][u][j]] < d[s][p[s][v - (1 << j) + 1][j]])
ki = p[s][u][j];
else
ki = p[s][v - (1 << j) + 1][j];
ll res = 2 * distSum - dist[s][i];
if (d[s][kt] < d[s][ki]) res += 2 * (dist[s][ki] - dist[s][kt]);
ans = min(ans, res);
}
cout << ans << '\n';
}
}
}
/*
https://cp-algorithms.com/graph/lca_farachcoltonbender.html#algorithm
O(nlogn): preprocess
O(1): query
https://www.geeksforgeeks.org/range-minimum-query-for-static-array/
*/
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int tc = 1; //cin >> tc;
while (tc--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 4004kb
input:
22 5 1 2 7 2 3 5 3 4 8 3 6 6 3 7 3 2 5 6 5 8 2 8 9 11 2 10 16 1 11 12 11 12 4 11 13 9 1 14 25 14 15 4 15 16 5 15 17 6 15 18 1 15 19 8 14 20 7 20 21 9 20 22 17 1 19 9 1 9 19 1 8 9 1 9 8 2 22 11
output:
316 293 293 impossible 314
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 7932kb
input:
100 100 1 2 289384 2 3 930887 2 4 692778 4 5 636916 4 6 747794 4 7 238336 4 8 885387 8 9 760493 8 10 516650 8 11 641422 8 12 202363 8 13 490028 8 14 368691 8 15 520060 8 16 897764 16 17 513927 16 18 180541 16 19 383427 16 20 89173 16 21 455737 16 22 5212 16 23 595369 16 24 702568 16 25 956430 16 26 ...
output:
103526917 103484292 106288816 104379596 104405611 104775512 105434682 105291604 103838430 105371370 104677980 104175650 105894571 104509242 103971939 105376499 105223283 104153426 105082245 105413188 104130613 104800548 106846555 104138329 103769253 105456754 104044745 104385328 106973740 105460460 ...
result:
ok 100 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3872kb
input:
10 6 1 2 4 2 3 7 2 4 8 4 5 6 4 6 4 4 7 6 4 8 7 8 9 3 8 10 10 3 8 1 10 4 7 1 7 3 7 2 9 8 10 3 4 1 6
output:
99 78 97 87 88 93
result:
ok 6 lines
Test #4:
score: 0
Accepted
time: 2ms
memory: 3872kb
input:
10 9 9 2 5 9 1 6 9 4 97 9 7 2 9 8 42 9 10 11 9 6 77 9 3 14 9 5 9 4 7 10 7 3 8 8 7 9 1 4 8 10 7 4 7 1 2 10 1 5 10 7 2 8 4 10
output:
352 427 impossible 443 418 427 418 418 407
result:
ok 9 lines
Test #5:
score: 0
Accepted
time: 2ms
memory: 3608kb
input:
9 9 2 3 48 9 5 31 7 3 97 4 3 16 1 7 24 5 3 82 8 2 51 6 4 33 1 2 8 3 6 8 1 6 3 9 5 6 2 6 4 5 6 1 9 6 4 2 8 9 4 9 2
output:
530 643 impossible 530 impossible 561 impossible 595 627
result:
ok 9 lines
Test #6:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
8 9 1 7 51 7 6 86 2 3 62 8 4 72 5 6 17 4 1 75 3 1 41 2 3 7 5 8 4 6 1 3 8 6 2 4 2 7 8 5 6 2 1 5 7 1 6 6 7 8
output:
551 impossible 524 558 579 impossible 551 705 524
result:
ok 9 lines
Test #7:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
9 9 5 4 13 9 2 10 1 9 25 7 6 34 4 2 77 3 8 67 8 1 57 6 9 100 6 4 1 4 1 7 3 2 9 4 9 7 7 9 3 6 2 1 2 8 4 8 6 2 6 5 9
output:
517 545 impossible 530 483 517 642 584 impossible
result:
ok 9 lines
Test #8:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
10 10 2 4 26 9 8 39 4 5 88 6 3 70 7 6 7 10 4 41 8 3 57 1 6 15 5 6 9 2 8 6 3 9 1 5 7 8 4 7 8 7 6 4 2 7 3 6 8 2 5 4 10 4 8 9 1 5 9
output:
impossible 496 529 441 531 415 566 529 441 523
result:
ok 10 lines
Test #9:
score: 0
Accepted
time: 2ms
memory: 3868kb
input:
10 9 3 2 6 2 1 18 8 1 44 1 9 42 6 3 72 10 8 46 7 10 93 5 3 11 4 10 13 7 3 1 8 2 5 7 5 4 5 10 8 3 1 4 6 4 3 10 1 6 5 10 8 2 10 4
output:
impossible 550 584 impossible 483 impossible 504 impossible 489
result:
ok 9 lines
Test #10:
score: -100
Time Limit Exceeded
input:
2000 199998 126 244 481188299 718 1159 740107180 1327 1250 248943862 977 1092 780169400 826 27 932696654 1668 638 478193038 229 174 176675890 1251 646 843918836 102 1973 593920932 236 218 165399894 760 151 890198591 232 502 10739184 1961 1409 45917915 548 1840 974742709 1096 630 280975617 1110 1048 ...
output:
1266421864327 impossible 1003453161105 1017793822920 1056758437569 impossible 1249128162612 1233756636475 1354563020262 1275484665657 impossible impossible 1644448395794 impossible impossible impossible 1305598243001 1730425595360 1090858373772 1180211385304 1235543994987 1894692656465 impossible 12...