# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Name |
---|
Hello, since this question is about a DAG, you can first topological sort the graph and then do longest path dp.
Thanks, DP worked. But what do you mean by topologically sorting the graph, do we find the topological ordering (of vertices) and then do dfs according to that order? Here I don't see why require that as we have to go from Vertex-1 to vertex-n.
Yes, topologically ordering the vertices.
I think in general doing DP on DAG, the order from top sorting is the order you process the nodes in. Yes, 1 is start and n is end but you don’t know the order of the nodes in the middle and processing them in a wrong order should give you a wrong answer.
For example if your graph is [{1, 3}, {3, 2}, {2, 4}] and you process the nodes in the order 1, 2, 3, 4 then you may get dp[1] = 0, dp[2]=inf, dp[3]=1, dp[4]=inf since when trying to fill in dp[2] we haven’t filled in dp[3] yet. Then when we try to fill in dp[4], we see that we have dp[2] incorrectly filled in with inf so we fill in dp[4] with inf+1