a739. 道路架設

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

給定一個有根樹,詢問兩個節點 $(u, v)$ 的距離,點數最多 $V = 2000000$

Sample Input

1
2
3
4
5
6
7
8
4
1 3
1 7
2 5
3
1 4
2 3
1 4

Sample Output

1
2
8
10

Solution

明顯地不可能使用 LCA-RMQ$O(Q \log V)$ 在線詢問,要套用 tarjan 算法中的 Offline-LCA,但 tarjan 算法靠的是遞迴遍歷,在 V = 20000000 很容易發生 stackoverflow,為了解決這一點採用非遞迴的方式實作。

這一題還有特別卡的地方,有向邊的儲存,之前 tarjan 都用在無向樹上,所以普遍都會儲存 $2 E$ 條邊,這一題必須只儲存 $E$ 條邊,常數卡得緊,不掛上優化輸入時間限制是在 TLE 邊緣。

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <bits/stdc++.h>
using namespace std;
const int MAXV = 2000005;
const int MAXQ = 100005;
const int MAXE = 2000005;
class LCA {
public:
struct Edge {
int v;
Edge *next;
};
struct QEdge {
int qid, u;
QEdge *next;
};
Edge edge[MAXE], *adj[MAXV], *arc[MAXV];
QEdge qedge[MAXQ<<1], *qadj[MAXV], *qarc[MAXV];
int e, eq, n;
int parent[MAXV], weight[MAXV], visited[MAXV], LCA[MAXQ];
void init(int n) {
e = eq = 0, this->n = n;
for (int i = 0; i < n; i++)
adj[i] = NULL, qadj[i] = NULL;
}
void addDedge(int x, int y) {
edge[e].v = y, edge[e].next = adj[x], adj[x] = &edge[e++];
}
void addQuery(int x, int y, int qid) {
qedge[eq].qid = qid, qedge[eq].u = y, qedge[eq].next = qadj[x], qadj[x] = &qedge[eq++];
qedge[eq].qid = qid, qedge[eq].u = x, qedge[eq].next = qadj[y], qadj[y] = &qedge[eq++];
}
void offline(int root) {
tarjan(root);
}
private:
int findp(int x) {
return parent[x] == x ? x : (parent[x] = findp(parent[x]));
}
int joint(int x, int y) {
x = findp(x), y = findp(y);
if(x == y) return 0;
if(weight[x] > weight[y])
weight[x] += weight[y], parent[y] = x;
else
weight[y] += weight[x], parent[x] = y;
return 1;
}
struct Node {
int u, p, line;
Node(int a = 0, int b = 0, int c = 0):
u(a), p(b), line(c) {}
};
void tarjan(int root) {
for (int i = 0; i < n; i++)
arc[i] = adj[i], qarc[i] = qadj[i], visited[i] = 0;
stack<Node> stk;
Node u;
int x, y;
stk.push(Node(root, -1, 0));
parent[root] = root;
while (!stk.empty()) {
u = stk.top(), stk.pop();
if (u.line == 0) {
if (arc[u.u]) {
y = arc[u.u]->v, arc[u.u] = arc[u.u]->next;
stk.push(u);
parent[y] = y;
stk.push(Node(y, u.u, 0));
} else {
visited[u.u] = 1;
u.line++;
stk.push(u);
}
} else {
if (qarc[u.u]) {
x = qarc[u.u]->qid, y = qarc[u.u]->u, qarc[u.u] = qarc[u.u]->next;
stk.push(u);
if (visited[y])
LCA[x] = findp(y);
} else {
if (u.p != -1)
parent[findp(u.u)] = u.p;
}
}
}
}
} lca;
int A[MAXQ], B[MAXQ], dist[MAXV];
long long C[MAXV];
struct Node {
int u, line;
Node(int a = 0, int b = 0):
u(a), line(b) {}
};
void dfs(int root) {
for (int i = 0; i < lca.n; i++)
lca.arc[i] = lca.adj[i];
stack<Node> stk;
Node u;
int x, y;
dist[0] = 0, C[0] = 0;
stk.push(Node(root, 0));
while (!stk.empty()) {
u = stk.top(), stk.pop();
if (lca.arc[u.u] != NULL) {
y = lca.arc[u.u]->v;
lca.arc[u.u] = lca.arc[u.u]->next;
stk.push(u);
dist[y] = dist[u.u]+1;
C[y] += C[u.u];
stk.push(Node(y, 0));
}
}
}
namespace mLocalStream {
inline int readchar() {
const int N = 1048576;
static char buf[N];
static char *p = buf, *end = buf;
if(p == end) {
if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF;
p = buf;
}
return *p++;
}
inline int ReadInt(int *x) {
static char c, neg;
while((c = readchar()) < '-') {if(c == EOF) return 0;}
neg = (c == '-') ? -1 : 1;
*x = (neg == 1) ? c-'0' : 0;
while((c = readchar()) >= '0')
*x = (*x << 3) + (*x << 1) + c-'0';
*x *= neg;
return 1;
}
class Print {
public:
static const int N = 1048576;
char buf[N], *p, *end;
Print() {
p = buf, end = buf + N - 1;
}
void printInt(int x, char padding) {
static char stk[16];
int idx = 0;
stk[idx++] = padding;
if (!x)
stk[idx++] = '0';
while (x)
stk[idx++] = x%10 + '0', x /= 10;
while (idx) {
if (p == end) {
*p = '\0';
printf("%s", buf), p = buf;
}
*p = stk[--idx], p++;
}
}
static inline void online_printInt(int x) {
static char ch[16];
static int idx;
idx = 0;
if (x == 0) ch[++idx] = 0;
while (x > 0) ch[++idx] = x % 10, x /= 10;
while (idx)
putchar(ch[idx--]+48);
}
~Print() {
*p = '\0';
printf("%s", buf);
}
} bprint;
}
int main() {
int N, Q, P, c;
mLocalStream::ReadInt(&N);
lca.init(N);
for (int i = 1; i < N; i++) {
mLocalStream::ReadInt(&P);
mLocalStream::ReadInt(&c);
C[i] = c, P--;
lca.addDedge(P, i);
}
mLocalStream::ReadInt(&Q);
for (int i = 0; i < Q; i++) {
mLocalStream::ReadInt(A+i);
mLocalStream::ReadInt(B+i);
A[i]--, B[i]--;
lca.addQuery(A[i], B[i], i);
}
dfs(0);
lca.offline(0);
int lazy = 0;
long long d1, d2;
for (int i = 0; i < Q; i++) {
if (A[i] == B[i] || lca.LCA[i] != A[i])
lazy++;
else {
d1 = dist[A[i]] + dist[B[i]] - 2*dist[lca.LCA[i]];
d2 = C[A[i]] + C[B[i]] - 2*C[lca.LCA[i]];
printf("%lld\n", d1*lazy + d2);
}
}
return 0;
}