I want the ask aquestion. "You have a DAG(directed acyclic graph) which is the contain n(1<=n<=300.000) nod. How many nodes can you go if you will start nod i(1<=i<=n). You must find answer for all nod."
# | 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 |
---|
Problem statement is not clear. Do you have a link to the original problem?
It seems to be "for each i, count vertices that can be reached from vertex i", which is at least O(N2). Whoops.
we can use dfs like idea i think. For every node there will be a mask which will indicate which nodes are reachable from this node. To calculate the mask of a node 'U' we can recursively calculate the info (mask) for the child nodes of 'U'. And to find the mask of U we can use bitwise OR operation.
The problem is as total node is 300000 we have to use an array of integers to define the mask. if we use int then the mask array will be of size 300000/32 = 9375. if we use long long then the mask array will be of size 300000/64 = 4688. is this a Online judge problem? may be the limit is not that big. can you provide the link if it is an online judge problem.
Then I guess ML for this problem is 10Gb.
Ooow. it's very cool question and solution. But this solution isn't fast.
Your question is exactly the same with http://www.spoj.com/problems/DAGCNT2/.
And does this problem actually have a better solution than O(N * M)? Or is it just about constant optimizations?
Someone told me they had proved the lower bound is OmegaO(|V||E|). But I didn't read the paper.
Actually, the problem setter said that his solution was based on std::bitset.
Thank you for answering, I was pondering on that question for a while. It does seem intuitive that you can't do better than that, a proof does seem complicated indeed though. Could you please link to the paper if you come across it? :)
The "someone" just told me. :) I didn't have the exact paper. Sorry about that.
In the problem on SPOJ, n=20000, very smaller than n in this problem. Time limit DAGCNT2 is 26s, and memory limit is 256MB. If we use the same implementation to submit on above problem. TL should be 390s (6.5 minutes) and ML should be 3840MB (3.8GB).
I think you can use a DP approach for this problem. I should be O( |V| + |E| ).
For each node that hasn't been calculate you run a dfs( u ). Where dfs(u) will return how many nodes you can reach from there and for each of its children you will run a dfs( ) that will save the number of nodes that each of its children can reach.
Well, this is my idea. Sorry if i have some grammar or spelling mistakes. I am trying to fix it.
This algorithm will not run correctly. for example we have four nodes. first node have two children which are second and third node. Second and third node have a child which is fourth node. fourth node haven't got a child. we called dfs( 1 ).
dfs(1) = dfs(2)+dfs(3)+1.
dfs(2) = dfs(4)+1 = 2.
dfs(3) = dfs(4)+1 = 2.
dfs(1) = 5.
for first node we count the fourth node twice. normally dfs(1)=4 but we find dfs(1)=5
So, I think that a solution could be that if you actually reach that node you don't have to count it.
In this example you will have:
Well, I think that should work.
And now answer for 3 is incorrect.
Someone told me a randomized algorithm can run really fast, and for at least 90% of
n
, its answers' relative error is at most 10%. However, I could not remember the algorithm......Process vertices in inverse topological order.
For each vertex the result is 1 + sum of results for children
O(N + M) or I missed something?
for 4 node
roads
1 -> 2
1 -> 3
2 -> 4
3 -> 4
u counting two times node 4 for calculate node 1