-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvirtualTree.java
More file actions
188 lines (147 loc) · 3.74 KB
/
virtualTree.java
File metadata and controls
188 lines (147 loc) · 3.74 KB
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
178
179
180
181
182
183
184
185
186
187
188
//lca and graph inits
int[][] g ;
int[] lvl;
int[][] par ;
int dfs_time ;
int[] st,end;
int n,m;
//virtual tree inits
int[] subTreeCnt;
ArrayList<ArrayList<Integer>> adjVirtual;
TreeSet<Integer> vert ;
HashSet<Integer> imp;
void solve() throws Exception {
/*
10
1 2
1 3
2 4
2 5
5 6
6 7
5 8
3 9
3 10
2
3
7 8 10
2
7 8
ans : 14,3
Problem Description : for given set of k nodes find for all pair of vertices (unordered) sum of distance between them
Youtube Tutorial : https://www.youtube.com/watch?v=czySm7bUHgY
*/
n=ni(); m=n-1;
// everything is 0-based indexing
int[] to = new int[m]; int[] from = new int[m];
for(int i=0;i<m;++i){
int x=ni()-1,y=ni()-1;
from[i]=x;to[i]=y;
}
g = ng(n,n-1,from,to,true);
init();
int q=ni();
adjVirtual = new ArrayList<>();
subTreeCnt = new int[n];
for(int i=0;i<n;++i) adjVirtual.add(new ArrayList<>());
while(q-->0){
int k=ni();
vert= new TreeSet<>((Integer a,Integer b)->Integer.compare(st[a],st[b]));
imp= new HashSet<>();
while(k-- > 0){
int x=ni()-1;
vert.add(x);
imp.add(x);
}
int root = createVirtualTree();
pn(adjVirtual);
pn(solve_dfs(root,-1));
for(int x: vert) {
adjVirtual.get(x).clear();
subTreeCnt[x] = 0;
}
}
}
long solve_dfs(int s,int p){
int ans = 0;
subTreeCnt[s] = (imp.contains(s)?1:0);
for(int x: adjVirtual.get(s)){
if(x==p) continue;
ans += solve_dfs(x,s);
subTreeCnt[s] += subTreeCnt[x];
}
// adding edge contribution
if(p!=-1){
long w = (lvl[s] - lvl[p]);
//pn("for edge "+s+" "+p+ " w: "+w);
ans += w*subTreeCnt[s]*(imp.size()-subTreeCnt[s]);
}
return ans ;
}
int createVirtualTree(){
// creates virtual tree and returns root of virtual tree
ArrayList<Integer> al =new ArrayList<>(vert);
for(int i=0;i<al.size()-1;++i){
vert.add(lca(al.get(i),al.get(i+1)));
}
al = new ArrayList<>(vert);
LinkedList<Integer> stk = new LinkedList<>();
stk.add(al.get(0));
for(int i=1;i<al.size();++i){
int u = al.get(i);
// while(stk.size() >=2 && !ancestor(stk.peekLast(),al.get(i))){
while(!ancestor(stk.peekLast(),u)){
// remove vertices till you reach right parent of given al.get(i) vertex
// while removing also add edges to virtual tree of removed vertices
// you will surely get atleast one vertex as ancestor because initially we have added global lca in stk
int last = stk.pollLast();
adjVirtual.get(last).add(stk.peekLast());
adjVirtual.get(stk.peekLast()).add(last);
}
stk.add(u);
}
while(stk.size()>=2){
int last = stk.pollLast();
int u = stk.peekLast();
adjVirtual.get(last).add(u);
adjVirtual.get(u).add(last);
}
return stk.pollLast();
}
void init(){
par = new int[n][20];
lvl = new int[n];
st = new int[n]; end = new int[n];
dfs_time = 0 ;
// here it is necessary to make par of root as root cause all parents of root at each height is root only
lca_dfs(0,0);
}
void lca_dfs(int s,int p){
st[s] = dfs_time ;
dfs_time++;
par[s][0] = p;
for(int i=1;i<20;++i){
par[s][i] = par[par[s][i-1]][i-1];
}
for(int x: g[s]){
if(x==p) continue;
lvl[x] = lvl[s] + 1 ;
lca_dfs(x,s);
}
end[s] = dfs_time ;
dfs_time++;
}
boolean ancestor(int u,int v){
// is u is some ancestor of v
return (st[v] > st[u] && end[v] < end[u]);
}
int lca(int u,int v){
if(ancestor(u,v)) return u;
if(ancestor(v,u)) return v;
for(int i=19;i>=0;--i){
if(!ancestor(par[u][i],v)){
u = par[u][i];
}
}
return par[u][0];
}