Description
给定你一棵n
个点的树,q
次询问,每次询问以切边的方式使给出的关键点与根节点不联通的最小代价。
$ n leq 250000, sum k <= 500000 $
Solution
虚树的一道基本的应用题。 考虑一个暴力的DP
, DP[i]
表示切断其子树内的关键点的最小代价。那么显然有:(dp[u] = sum_{v | v~is~a~son ~of~ u} min(dp[v], UpsideMin[v])),其中upsideMin[u]
,表示u
到根节点上的链上的最小的边权。
显然这样做是对的。但时间复杂度过不去。考虑一个点,如果它不是关键点,也不是一部分关键点的LCA
,以及根节点。那么我们在dp
时其实可以忽略它(因为更新u时只与v有关),那么我们只要把一部分树中的点抽离出来dp
。
但这样每次都要建立一次树,考虑把他们(KeyVertex
)按照Euler
序排序, 因为Euler序是真实的出入栈的记录。又DFS的本质是出入栈以及对栈顶元素进行操作。所以直接用栈进行模拟即可。
注意, 我们求LCA
时不需要全部都求,只要把关键点按照DFS
序排序后相邻之间求LCA
,因为要去掉无关的点,而不选择LCA
又会算重,所以只需要按照dfs
序排序后相邻点求lca
,这样也只有m-1个lca
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The massage in line %d, Function %s: %s
", __LINE__, __FUNCTION__, s)
typedef long long LL;
typedef long double LD;
const int BUF_SIZE = (int)1e6 + 10;
struct fastIO {
char buf[BUF_SIZE], buf1[BUF_SIZE];
int cur, cur1;
FILE *in, *out;
fastIO() {
cur = BUF_SIZE, in = stdin, out = stdout;
cur1 = 0;
}
inline char getchar() {
if(cur == BUF_SIZE) fread(buf, BUF_SIZE, 1, in), cur = 0;
return *(buf + (cur++));
}
inline void putchar(char ch) {
*(buf1 + (cur1++)) = ch;
if (cur1 == BUF_SIZE) fwrite(buf1, BUF_SIZE, 1, out), cur1 = 0;
}
inline int flush() {
if (cur1 > 0) fwrite(buf1, cur1, 1, out);
return cur1 = 0;
}
}IO;
#define getchar IO.getchar
#define putchar IO.putchar
int read() {
char ch = getchar();
int x = 0, flag = 1;
for(;!isdigit(ch); ch = getchar()) if(ch == '-') flag *= -1;
for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(LL x) {
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
void putString(char s[], char EndChar = '
') {
rep(i, 0, strlen(s) - 1) putchar(*(s + i));
if(~EndChar) putchar(EndChar);
}
#define Maxn 250009
struct edge {
int to, nxt, w;
}g[Maxn << 1];
int n, head[Maxn], e;
int size[Maxn], fa[Maxn], top[Maxn], dfn[Maxn], efn[Maxn], clk, son[Maxn], dep[Maxn];
int beg[Maxn], lst[Maxn], Euler_clk;
int UpMin[Maxn];
namespace INIT{
void dfs_init(int u, int pre) {
dep[u] = dep[pre] + 1; fa[u] = pre;
size[u] = 1;
beg[u] = ++Euler_clk;
for(int i = head[u]; ~i; i = g[i].nxt) {
int v = g[i].to;
if(v != pre) {
UpMin[v] = min(UpMin[u], g[i].w);
dfs_init(v, u);
size[u] += size[v];
son[u] = (son[u] == -1 || size[son[u]] < size[v]) ? v : son[u];
}
}
lst[u] = ++Euler_clk;
}
void dfs_link(int u, int _top) {
top[u] = _top;
dfn[u] = ++clk; efn[clk] = u;
if(~son[u]) dfs_link(son[u], _top); else return ;
for(int i = head[u]; ~i; i = g[i].nxt) {
int v = g[i].to;
if(v != fa[u] && v != son[u]) dfs_link(v, v);
}
}
void add(int u, int v, int w) {
g[++e] = (edge){v, head[u], w}, head[u] = e;
}
void Main() {
clar(UpMin, 0x3f);
clar(head, -1), clar(son, -1);
n = read();
rep(i, 1, n - 1) {
int u = read(), v = read(), w = read();
add(u, v, w), add(v, u, w);
}
dfs_init(1, 0);
dfs_link(1, 1);
}
}
namespace SOLVE{
vector <int> keyVertex, Tmp;
stack <int> s_stack;
LL dp[Maxn], instack[Maxn];
int LCA(int u, int v) {
while(top[u] != top[v]) {
if(dep[top[u]] < dep[top[v]]) swap(u, v);
u = fa[top[u]];
}
return dep[u] < dep[v] ? u : v;
}
int cmp(int u, int v) {
return (u > 0 ? beg[u] : lst[-u]) < (v > 0 ? beg[v] : lst[-v]);
}
LL calc() {
LL res = 0;
for(auto i : keyVertex) {
dp[i] = UpMin[i];
instack[i] = 1;
Tmp.push_back(i);
}
sort(keyVertex.begin(), keyVertex.end(), cmp);
rep(i, 1, keyVertex.size() - 1) {
int u = LCA(keyVertex[i], keyVertex[i - 1]);
if(!instack[u]) {
instack[u] = 1;
Tmp.push_back(u);
}
}
if(!instack[1]) {
instack[1] = 1;
Tmp.push_back(1);
}
rep(i, 0, Tmp.size() - 1) Tmp.push_back(-Tmp[i]);
sort(Tmp.begin(), Tmp.end(), cmp);
rep(i, 0, Tmp.size() - 1) {
if(Tmp[i] > 0) s_stack.push(Tmp[i]);
else {
int u = s_stack.top(); s_stack.pop();
instack[u] = 0;
int Papa = s_stack.top();
if(u != 1) dp[Papa] += min(1ll * UpMin[u], dp[u]);
else { res = dp[u]; }
dp[u] = 0;
}
}
keyVertex.clear();
Tmp.clear();
return res;
}
void Main() {
rep(i, 1, read()) {
rep(j, 1, read()) keyVertex.push_back(read());
write(calc()), putchar('
');
}
}
}
int main() {
#ifdef Qrsikno
freopen("BZOJ2286.in", "r", stdin);
freopen("BZOJ2286.out", "w", stdout);
#endif
INIT :: Main();
SOLVE :: Main();
#ifdef Qrsikno
debug("
Running time: %.3lf(s)
", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return IO.flush();
}