0%

虚树

其实虚树这一套没什么,就是一个板子。主要解决的是给一个树,然后对树上的某个点集搞些什么操作啊询问啊,之类的问题。 就是把原树的dfs序搞出来,然后把要搞的点和lca拿出来,搞一棵新的树,用单调栈来时限,其实就是一个模板。 例题是bzoj 3611

给定一棵树,然后询问一个点集的所有互相之间的路径和以及最长路径最短路径

把虚树建出来,然后就树dp随便维护最长链最短链就好了,很显然。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int MAXN = 1000005;
vector<int> Map[MAXN];
typedef long long LL;
LL asum,amin,amax;
char buf[1000000],*p1=buf,*p2=buf;
inline char nc(){
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline bool rea(int & x){
char c=nc();x=0;
if(c==EOF) return false;
for(;!isdigit(c);c=nc())
{
if(c==EOF) return false;
}
for(;isdigit(c);x=x*10+c-'0',c=nc());
return true;
}
inline bool rea(LL & x){
char c=nc();x=0;
if(c==EOF) return false;
for(;!isdigit(c);c=nc());
for(;isdigit(c);x=x*10+c-'0',c=nc());
return true;
}
class voidTree{
public:
#define mk make_pair
static const int MAXN = 1000005,LOG = 22;
int dfn[MAXN],out[MAXN],dep[MAXN],f[MAXN][LOG + 1];
bool flag[MAXN];
vector<pair<int,LL> > tree[MAXN];
vector<pair<int,int> > a;
stack<int> s;
int tot,root,n;
LL smin[MAXN],smax[MAXN],ssum[MAXN],g[MAXN],ct[MAXN];
void set(int _n)
{
n = _n;
root = 1; tot = 0; dfs(1,0);
for (int i=1;i<=LOG;i++)
{
for (int j=1;j<=n;j++) f[j][i] = f[f[j][i-1]][i-1];
}
}
void dfs(int u,int fa)
{
f[u][0] = fa;
dfn[u] = ++tot;
dep[u] = dep[fa] + 1;
for (int i=0;i<Map[u].size();i++)
{ int v = Map[u][i];
if (v!=fa) dfs(v,u);
}
out[u] = tot;
}
int lca(int u,int v)
{
if (dep[u] < dep[v]) swap(u,v);
for (int i=LOG;i>=0;i--)
if (dep[f[u][i]] >= dep[v]) u = f[u][i];
if (u == v) return u;
for (int i=LOG;i>=0;i--)
if (f[u][i] != f[v][i]){
u = f[u][i];
v = f[v][i];
}
return f[u][0];
}
inline bool check(int u,int v)
{
return (dfn[v]>=dfn[u]+1 && dfn[v]<=out[u]);
}
void dp(int u,int fa=-1)
{
ssum[u]=0; g[u]=0; ct[u]=0; smax[u]=-1; smin[u]=(1ll<<60);
if(flag[u])
{
smax[u]=0;
smin[u]=0;
}
for(int i=0;i<tree[u].size();i++)
{
pair<int,LL> x = tree[u][i];
int v=x.first;
if(v==fa) continue;
dp(v,u);
ssum[u]+=ct[v]*g[u]+ct[u]*(g[v]+ct[v]*x.second);
if(smax[u]!=-1&&smax[v]!=-1)
{
amax=max(amax,smax[u]+smax[v]+x.second);
}
if(smin[u]!=(1ll<<60)&&smin[v]!=(1ll<<60))
{
amin=min(amin,smin[u]+smin[v]+x.second);
}
smax[u]=max(smax[u],smax[v]+x.second);
smin[u]=min(smin[u],smin[v]+x.second);
if(flag[u])
{
ssum[u]+=g[v]+ct[v]*x.second;
}
g[u]+=g[v]+ct[v]*x.second;
ct[u]+=ct[v];
}
if(flag[u]) ct[u]++;
asum+=ssum[u];
}
void create(vector<int> &nodes)
{
a.clear();
root = nodes[0];
for (int i=0;i<nodes.size();i++) {
int x = nodes[i];
a.push_back(make_pair(dfn[x],x));
}
sort(a.begin(),a.end());
for (int i=1,m=a.size();i<m;i++) {
int x = lca(a[i].second,a[i-1].second);
a.push_back(make_pair(dfn[x],x));
}
sort(a.begin(),a.end());
for (int i=0;i<a.size();i++) {
pair<int,int> x = a[i];
flag[x.second] = false;
tree[x.second].clear();
}
for (int i=0;i<nodes.size();i++){ int x=nodes[i]; flag[x] = true;}
a.erase(unique(a.begin(),a.end()),a.end());
while (!s.empty()) s.pop();
for (int i=0;i<a.size();i++)
{
pair<int,int> x = a[i];
while (!s.empty() && !check(s.top(),x.second)) s.pop();
if (s.empty()) s.push(x.second);
else {
tree[s.top()].push_back(mk(x.second,dep[x.second] - dep[s.top()]));//s.top()是x.second的父亲
tree[x.second].push_back(mk(s.top(),dep[x.second] - dep[s.top()]));//非双向则注释掉
s.push(x.second);
}
}
}
} VT;
int n,q,k;
vector<int> tmp;
int main()
{
rea(n);
for(int i=1;i<n;i++)
{
int a,b;
rea(a); rea(b);
Map[a].pb(b);
Map[b].pb(a);
}
VT.set(n);
rea(q);
for(int i=1;i<=q;i++)
{
rea(k);
tmp.clear();
int u;
for(int j=1;j<=k;j++)
{
rea(u);
tmp.pb(u);
}
VT.create(tmp);
asum=0ll; amin=(1ll<<60); amax=0ll;
VT.dp(VT.root);
printf("%lld %lld %lldn",asum,amin,amax);
}
return 0;
}