Given a tree of n vertices (n <= 1e5) in which the edge is arbitrarily oriented. Also given q queries (q <= 1e5) in which a, b are entered. The problem is quickly answer whether there is a path from a to b.
Thank a lot for your help.
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3676 |
| 2 | jiangly | 3670 |
| 3 | Kevin114514 | 3657 |
| 4 | maroonrk | 3355 |
| 5 | Um_nik | 3353 |
| 6 | squareOf105 | 3346 |
| 7 | tourist | 3343 |
| 8 | strapple | 3324 |
| 9 | heuristica | 3322 |
| 10 | jiangbowen | 3291 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 158 |
| 2 | nik_exists | 150 |
| 2 | maspy | 150 |
| 4 | AmShZ | 143 |
| 5 | Um_nik | 141 |
| 6 | Errichto | 139 |
| 7 | adamant | 137 |
| 8 | BledDest | 133 |
| 8 | maroonrk | 133 |
| 10 | qwexd | 130 |
Given a tree of n vertices (n <= 1e5) in which the edge is arbitrarily oriented. Also given q queries (q <= 1e5) in which a, b are entered. The problem is quickly answer whether there is a path from a to b.
Thank a lot for your help.
| Name |
|---|



We should divide it in components by DFS (we give number to a component so every node in same component have same number) and than just check if their numbers are the same, it would mean that they are in the same component
Being in the same component doesn't guarantee path existing from node a to b. Consider the tree: 1 -> 2
If we started our dfs from node
1, nodes1and2would be in the same component. But we would answer query2 1wrong (reporting there's a path from2to1when in fact there isn't).Yes, yes right sorry i forgot that it's orienteered
find the lowest common ancestor of a and b, call it c. you can check if from a to c only contains up and from b to c only down or viceversa.
Use lca and prefixsum
The condition for a path to exist from $$$a$$$ to $$$b$$$ is:
$$$pref[a] - pref[p] = 0$$$ and $$$ pref[b] - pref[p] = depth[b] - depth[p]$$$
with $$$p$$$ is $$$lca(a, b)$$$