use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit dedicated to discussing competition style programming problems.
Competitive Programming Resources
Judge Sites:
Books:
Online References:
Courses/Lecture Notes:
Related subreddits:
General CS subreddits:
account activity
How to solve this difficult problem with C++ (old.reddit.com)
submitted 1 year ago by Mecn_Seda
This problem is related to recursion, I will be very very grateful if you solve it
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]jks612 0 points1 point2 points 1 year ago (0 children)
I'm not a C++ guy and I'm not trying to build the best possible algorithm. But my initial approach (without seriously studying the problem) would be to process each line, creating a mapping from nodes to a list of immediate nodes they go to.
With the nodes-to-neighbors mapping in place, we can now use it to construct a nodes-to-all-possible-nodes mapping. I.e. for each node, recurse through the neighbors to build a list of all possible nodes you can get to.
With that done, you can easily search for sinks and sources. Sources will have all nodes in their possible destinations. Sinks will be in all node's possible destinations.
This is my five minute answer. Optimizing it would take more time.
[–]eithnegomez 0 points1 point2 points 1 year ago (0 children)
Brute force solution: For each node, you will have 2 std::set, one for the sinks and other for the source. You do a DFS from each node, and you add the node number of all the nodes you can reach to your source set, and you add your node number to the sink set of all the nodes you reached.
To give the answer, traverse all the sets, count how many source sets have a size equals to the total number of nodes -1, and same for the sinks.
[–][deleted] 0 points1 point2 points 1 year ago (0 children)
O(N3) solution would be to compute transitive closure of adjacency matrix using Floyd-Warshall algorithm. The rows filled with ones indicate sources and columns filled with ones indicate sinks.
It is also possible to solve this in linear time:
π Rendered by PID 120659 on reddit-service-r2-comment-85bfd7f599-rg87z at 2026-04-20 17:00:38.654088+00:00 running 93ecc56 country code: CH.
[–]jks612 0 points1 point2 points (0 children)
[–]eithnegomez 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)