QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#209451 | #7102. Live Love | PetroTarnavskyi# | RE | 0ms | 0kb | C++17 | 2.2kb | 2023-10-10 15:12:09 | 2023-10-10 15:12:10 |
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int INF = 1'000'000'447;
const LL LINF = (LL)INF * INF;
const int N = 100'447;
const int LOG = 17;
vector<PII> g[N];
LL d[N];
LL cost[N];
bool red[N];
int tin[N];
int tout[N];
int up[LOG][N];
int T = 0;
void dfs(int v, LL c, LL h, int p)
{
if (red[v])
c = 0;
cost[v] = c;
d[v] = h;
tin[v] = T++;
up[0][v] = p;
FOR (i, 1, LOG)
{
up[i][v] = up[i - 1][up[i - 1][v]];
}
for (auto [to, w] : g[v])
{
if (to != p)
{
dfs(to, c + w, h + w, v);
}
}
tout[v] = T++;
}
bool is_par(int p, int v)
{
return tin[p] <= tin[v] && tout[v] <= tout[p];
}
int lca(int v, int u)
{
if (is_par(v, u))
return v;
RFOR (i, LOG, 0)
{
if (!is_par(up[i][v], u))
v = up[i][v];
}
return up[0][v];
}
void solve()
{
int n, m, q;
cin >> n >> m >> q;
FOR (i, 0, m)
{
int x;
cin >> x;
red[x - 1] = true;
}
FOR (i, 0, n - 1)
{
int a, b, w;
cin >> a >> b >> w;
a--, b--;
g[a].PB({b, w});
g[b].PB({a, w});
}
dfs(0, 0, 0, 0);
FOR (i, 0, q)
{
int k;
cin >> k;
VI v(k);
FOR (j, 0, k)
{
cin >> v[j];
v[j]--;
}
sort(ALL(v), [&](int a, int b)
{
return cost[a] > cost[b];
});
LL ans = cost[v[0]];
int lc = v[0];
LL D = d[v[0]];
FOR (j, 0, k)
{
lc = lca(lc, v[j]);
D = max(D, d[v[j]]);
LL x = D - d[lc];
if (j + 1 != k)
x = max(x, cost[v[j + 1]]);
ans = min(ans, x);
}
cout << ans << '\n';
}
FOR (i, 0, n)
{
g[i].clear();
cost[i] = 0;
d[i] = 0;
FOR (j, 0, LOG)
up[j][i] = 0;
tin[i] = 0;
tout[i] = 0;
red[i] = false;
}
T = 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(15);
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
5 5 4 100 50 252 52 3 0 10 10