//Bismillahir-Rahmanir-Rahim
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
# define pii pair <int, int>
# define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 5e4 + 2;
const ll inf = 2e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
const ll P = 67;
using namespace std;
int n, q, d[maxn], timer;
int tin[maxn], tout[maxn];
vector <pii> g[maxn];
int up[maxn][18];
void dfs (int v = 1, int pa = 1)
{
up[v][0] = pa;
for (int j = 1; j <= 16; ++j)
{
up[v][j] = up[up[v][j - 1]][j - 1];
}
tin[v] = ++timer;
for (pii it : g[v])
{
int to = it.ff, w = it.ss;
if (to == pa) continue;
d[to] = d[v] + w;
dfs(to, v);
}
tout[v] = timer;
}
bool anc (int u, int v)
{
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
int lca (int u, int v)
{
if (anc(u, v)) return u;
if (anc(v, u)) return v;
for (int j = 16; j >= 0; --j)
{
if (!anc(up[u][j], v))
{
u = up[u][j];
}
}
return up[u][0];
}
int dist (int x, int y)
{
return d[x] + d[y] - 2 * d[lca(x, y)];
}
void ma1n (/* SABR */)
{
cin >> n;
for (int i = 1; i < n; ++i)
{
int u, v, w;
cin >> u >> v >> w;
u++, v++;
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs ();
cin >> q;
while (q--)
{
vector <pair <int, int> > x(5);
for (int i = 0; i < 5; ++i)
{
cin >> x[i].ss;
x[i].ss++;
x[i].ff = tin[x[i].ss];
}
sort(all(x));
int ans = 0;
for (int i = 0; i < 5; ++i)
{
int nxt = (i + 1) % 5;
int lc = lca(x[i].ss, x[nxt].ss);
ans += dist(x[i].ss, x[nxt].ss);
}
cout << ans / 2 << nl;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
int ttt = 1;
// cin >> ttt;
for (int test = 1; test <= ttt; ++test)
{
// cout << "Case " << test << ":" << '\n';
ma1n();
}
return 0;
}
// 998batrr | BbIWEJI 3A TObOU!!!
// tourist | BbIWEJI 3A TObOU!!!
Compilation message
roadsideadverts.cpp: In function 'void ma1n()':
roadsideadverts.cpp:102:17: warning: unused variable 'lc' [-Wunused-variable]
102 | int lc = lca(x[i].ss, x[nxt].ss);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
9908 KB |
Output is correct |
2 |
Correct |
26 ms |
11344 KB |
Output is correct |
3 |
Correct |
28 ms |
11360 KB |
Output is correct |
4 |
Correct |
29 ms |
11344 KB |
Output is correct |
5 |
Correct |
27 ms |
11340 KB |
Output is correct |
6 |
Correct |
27 ms |
11368 KB |
Output is correct |
7 |
Correct |
26 ms |
11316 KB |
Output is correct |
8 |
Correct |
28 ms |
11384 KB |
Output is correct |
9 |
Correct |
42 ms |
11376 KB |
Output is correct |
10 |
Correct |
35 ms |
11380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8176 KB |
Output is correct |
2 |
Correct |
22 ms |
10488 KB |
Output is correct |
3 |
Correct |
23 ms |
10468 KB |
Output is correct |
4 |
Correct |
23 ms |
10476 KB |
Output is correct |
5 |
Correct |
24 ms |
10484 KB |
Output is correct |
6 |
Correct |
40 ms |
10572 KB |
Output is correct |
7 |
Correct |
38 ms |
10572 KB |
Output is correct |
8 |
Correct |
22 ms |
10468 KB |
Output is correct |
9 |
Correct |
22 ms |
10584 KB |
Output is correct |
10 |
Correct |
23 ms |
10512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
30 ms |
9908 KB |
Output is correct |
3 |
Correct |
26 ms |
11344 KB |
Output is correct |
4 |
Correct |
28 ms |
11360 KB |
Output is correct |
5 |
Correct |
29 ms |
11344 KB |
Output is correct |
6 |
Correct |
27 ms |
11340 KB |
Output is correct |
7 |
Correct |
27 ms |
11368 KB |
Output is correct |
8 |
Correct |
26 ms |
11316 KB |
Output is correct |
9 |
Correct |
28 ms |
11384 KB |
Output is correct |
10 |
Correct |
42 ms |
11376 KB |
Output is correct |
11 |
Correct |
35 ms |
11380 KB |
Output is correct |
12 |
Correct |
19 ms |
8176 KB |
Output is correct |
13 |
Correct |
22 ms |
10488 KB |
Output is correct |
14 |
Correct |
23 ms |
10468 KB |
Output is correct |
15 |
Correct |
23 ms |
10476 KB |
Output is correct |
16 |
Correct |
24 ms |
10484 KB |
Output is correct |
17 |
Correct |
40 ms |
10572 KB |
Output is correct |
18 |
Correct |
38 ms |
10572 KB |
Output is correct |
19 |
Correct |
22 ms |
10468 KB |
Output is correct |
20 |
Correct |
22 ms |
10584 KB |
Output is correct |
21 |
Correct |
23 ms |
10512 KB |
Output is correct |
22 |
Correct |
28 ms |
9868 KB |
Output is correct |
23 |
Correct |
28 ms |
8508 KB |
Output is correct |
24 |
Correct |
32 ms |
10832 KB |
Output is correct |
25 |
Correct |
39 ms |
10940 KB |
Output is correct |
26 |
Correct |
40 ms |
10844 KB |
Output is correct |
27 |
Correct |
32 ms |
10828 KB |
Output is correct |
28 |
Correct |
36 ms |
10908 KB |
Output is correct |
29 |
Correct |
30 ms |
10836 KB |
Output is correct |
30 |
Correct |
44 ms |
10828 KB |
Output is correct |
31 |
Correct |
33 ms |
10828 KB |
Output is correct |