嘟嘟嘟
今天学了个虚树,觉得这东西挺奇特。
虚树简单来说就是保留每一次询问的关键点以及他们的lca,把其他的点从树中删去。这样如果每一次询问的复杂度为(O(n))的话,总复杂度就是(O(sum _ {i = 1} ^ {m} k_i))。
虚树的构建方法这里推fjzzq大佬的博客。
构建完虚树后,在虚树上dp。因为每一个点按dfs序排好了,所以可以省去递归,直接倒着dp,用每一个点的dp值更新他的父亲。
还有就是每一次只把用过的点清空,memset会超时。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2.5e5 + 5;;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, a[maxn];
struct Edge
{
int nxt, to, w;
}e[maxn << 1];
int head[maxn], ecnt = -1;
void addEdge(int x, int y, int w)
{
e[++ecnt] = (Edge){head[x], y, w};
head[x] = ecnt;
}
int dep[maxn], dfsx[maxn], cnt = 0;
int fa[21][maxn], Min[21][maxn];
inline void dfs(const int& now, const int& _f)
{
dfsx[now] = ++cnt;
for(int i = 1; (1 << i) <= dep[now]; ++i)
{
fa[i][now] = fa[i - 1][fa[i - 1][now]];
Min[i][now] = min(Min[i - 1][now], Min[i - 1][fa[i - 1][now]]);
}
for(int i = head[now], v; i != -1; i = e[i].nxt)
{
if((v = e[i].to) == _f) continue;
dep[v] = dep[now] + 1;
fa[0][v] = now; Min[0][v] = e[i].w;
dfs(v, now);
}
}
inline int LCA(int x, int y)
{
if(dep[x] < dep[y]) swap(x, y);
for(int i = 20; i >= 0; --i)
if(dep[x] - (1 << i) >= dep[y]) x = fa[i][x];
if(x == y) return x;
for(int i = 20; i >= 0; --i)
if(fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
return fa[0][x];
}
int vir[maxn]; //虚树点集
int st[maxn], top = 0;
int pa[maxn];
bool cmp(int x, int y) {return dfsx[x] < dfsx[y];}
inline void build(int& m)
{
vir[++m] = 1; top = 0;
sort(vir + 1, vir + m + 1, cmp);
int tp = m;
for(int i = 1; i <= tp; ++i)
{
int now = vir[i];
if(!top) {pa[st[++top] = now] = 0; continue;}
int lca = LCA(st[top], now);
while(dep[st[top]] > dep[lca])
{
if(dep[st[top - 1]] < dep[lca]) pa[st[top]] = lca;
--top;
}
if(lca != st[top])
{
vir[++m] = lca;
pa[lca] = st[top];
st[++top] = lca;
}
pa[st[++top] = now] = lca;
}
sort(vir + 2, vir + m + 1, cmp);
}
inline int dis(int x, int y) //两点间最小边权
{
int ret = INF;
if(dep[x] < dep[y]) swap(x, y);
for(int i = 20; i >= 0; --i)
if(dep[x] - (1 << i) >= dep[y]) ret = min(ret, Min[i][x]), x = fa[i][x];
if(x == y) return ret;
for(int i = 20; i >= 0; --i)
if(fa[i][x] != fa[i][y])
{
ret = min(ret, min(Min[i][x], Min[i][y]));
x = fa[i][x], y = fa[i][y];
}
return min(ret, min(Min[0][x], Min[0][y]));
}
bool vis[maxn];
int wi[maxn];
ll dp[maxn];
inline ll solve(const int& m)
{
for(int i = 2; i <= m; ++i) wi[vir[i]] = dis(vir[i], pa[vir[i]]);
for(int i = 1; i <= m; ++i) dp[vir[i]] = 0;
for(int i = m, v; i > 1; --i)
{
if(vis[v = vir[i]]) dp[pa[v]] += wi[v];
else dp[pa[v]] += min(dp[v], (ll)wi[v]);
}
return dp[1];
}
int main()
{
Mem(head, -1);
n = read();
for(int i = 1; i < n; ++i)
{
int x = read(), y = read(), w = read();
addEdge(x, y, w); addEdge(y, x, w);
}
dfs(1, 0);
int T = read();
while(T--)
{
m = read();
for(int i = 1; i <= m; ++i) vis[vir[i] = read()] = 1;
build(m);
write(solve(m)), enter;
for(int i = 1; i <= m; ++i) vis[vir[i]] = 0;
}
return 0;
}