# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | nor | 152 |
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