Have a DAG and we want that for each node v find the number of vertex that existence a path from v to that vertexes
for n <= 1e5
№ | Пользователь | Рейтинг |
---|---|---|
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 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 160 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 151 |
Have a DAG and we want that for each node v find the number of vertex that existence a path from v to that vertexes
for n <= 1e5
Название |
---|
I think simple traversing can helpful.
That is Good but the order is O(n^2);
Not really because you are using the adjacency-list representation of the graph.
Actually the answer is just to run DFS/BFS on that node and see how many nodes are visited in the process.
Complexity O(E + V)
That not for each vertex
Then you can find the condensation graph into SCC and run DFS to find the answer recursively. Complexity doesn't change
I have modified the code to make it run in O(n)
Can we really compute the number of vertices that can be reached from vertex v with this algorithm?
I think this graph is a counterexample.
(UPD: Sorry, the number with the red color font actually should be 6)
You can use a bit-parallel strategy.
Assuming bit operations for one word is O(1), and let |word| the word-length of your machine, and then the time complexity would be O(NM/|word|). The memory complexity to store the reachability can be O(N*|word|) if you only care about the reachability from |word| vertices at the same time.
In other words, in today's 64-bit environment, it's nearly 64x faster than the naive algorithm to check the reachability from each vertex to all vertices one by one.