arham_doshi's blog

By arham_doshi, history, 4 years ago, In English

I got bored with solving and wanted to do something which is related to cp and also very fun so i decided to write this tutorial.bare me for my bad English .

how to solve grid problems

trick

1) counting rooms

explanation
code

2) Labyrinth

explanation
code

3) Building Roads

explanation
code

4) Message Route

explanation
code

5) Building teams

explanation
code

6) Round Trip

explanation
code

7) Monsters

explanation
code

8) Shortest Routes I

explanation
code

9) Shortest Routes II

explanation
code

10) High scores

explanation
code

11) Flight Discounts

explanation
code

12)Finding Cycles

explanation
code

13) Flight Routes

explanation
code

14) round trip 2

explanation
code

15) Course Schedule

explanation
code

16) Longest flight routes

Explanation
code

17) Game Routes

Explanation
code

18) Investigation

Explanation
code

19) Planet queries I

explanation
code

other useful links


Hope this is helpfull to you . I will try to add more as I solve furthure.

  • Vote: I like it
  • +77
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by arham_doshi (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Problem links?

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Nice initiative.

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Very nice and helpful.

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it
»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

all hail ......editorialist arham_doshi

»
4 years ago, # |
  Vote: I like it +9 Vote: I do not like it
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi, thanks for the editorials. I am unable to understand the problem $$$High$$$ $$$Scores$$$. It says we can use a tunnel several times, that means if there is an edge with positive weight can't we use it several times(infinite times i.e. $$$a$$$ -> $$$b$$$ then $$$b$$$ -> $$$a$$$ ... ) and our answer will be -1?
and i think in the editorial you meant Bellman ford instead of Floyd Warshall.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Yes, we can use it hence you have to print -1 as said in the problem statement.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      If that is the case why our answer is 5 for sample? We can use the edge with positive weight from 1 and use it infinite times and our answer will be -1. Sorry if I misunderstood you:(

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        All tunnels are one-way tunnels...means it's a directed graph.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        yeah may be i got confused sed between bellman ford and floyd warshal.talking about sample it is a directed edge so after going from a to b you canot go back. " the tunnel starts at room a, ends at room b "

»
4 years ago, # |
  Vote: I like it +29 Vote: I do not like it

how to solve grid problems

Here is an additional trick for grid problems. Specifically for problems where the input is some kind of maze with # for "blocked" cells and . for "free" cells. I like the approach of using the dx and dy arrays but the possible function feels clunky to me in some problems.

Instead I put a "frame" made out of # around the whole grid. That is: when the input is

..#..#.
.#..#..
#.#..#.
.#...#.

instead I consider the input

#########
#..#..#.#
#.#..#..#
##.#..#.#
#.#...#.#
#########

It is really easy to implement. Initially fill the whole grid with #, then read the input.

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Nice one, i like it. I use possible at many places like when when solveing a dp probelm

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by arham_doshi (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by arham_doshi (previous revision, new revision, compare).

»
4 years ago, # |
Rev. 2   Vote: I like it -8 Vote: I do not like it

..

»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I'm having trouble with Building Roads problem. For some reason, I am getting TLE for test cases 6-11, although my solution is similar in idea to ones that have been accepted. How can I change my code so I don't get TLE?

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace __gnu_pbds;
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long ul;
 
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
 
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
 
typedef priority_queue<int> pq;
typedef priority_queue<int,vector<int>,greater<int>> pqg;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
 
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
 
 
void dfs(int start, vector<vi> adj, vector<bool>& visited){
	if(visited[start]) return;
	visited[start]=true;
	for(auto u:adj[start]) dfs(u,adj,visited);
	}
 
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
 
int n,m;
cin>>n>>m;
 
vector<vi> adj(n+1,vi());
for(int i=0; i<m; i++){
	int a,b;
	cin>>a>>b;
	adj[a].pb(b);
	adj[b].pb(a);
	}
 
vector<bool> visited(n+1,false);
 
vi add;
for(int i=1; i<=n; i++){
	if(!visited[i]){
		dfs(i,adj, visited);
		add.pb(i);
		}
	}
 
cout<<add.size()-1<<endl;
if(add.size()>1){
	for(int i=1; i<add.size(); i++){
		cout<<1<<" "<<add[i]<<endl;
		}
	}	
 
}
  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    In case someone reads this, I had the same problem. I'm not experienced with C++ so I struggled to catch the problem. It turns out adj in dfs function is passed as copy, not by reference. Changing that to vector &adj, or just using global variables instead works, because you won't copy the adj vector every time you call dfs function

»
4 years ago, # |
Rev. 2   Vote: I like it +18 Vote: I do not like it

I'm currently making Video editorials for tree section of CSES.
Interested people can check : CSES (DP ON) TREE ALGORITHMS

I'm hoping to finish it in around a week, will be happy to know if people find it helpful :)

UPD : added first 5, do share suggestions if any.

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

This is really helpful.

Thank You arham_doshi !!

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

13) flight routes give TLE as its O(nmk)

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Its o(m*k), i was writing there max(n, m)*k

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I calculated the complexity. Shouldn't it be O(k(m+nlogn))?. It is still giving me TLE for the above algorithm.

      Secondly, why did you run a classical djikstra before? you are not using the distance array in the algorithm.

      • »
        »
        »
        »
        4 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        i am finding all the possible paths . you can think it as bfs . it i optimised brute force where you can only take take vertex at max k time. i was solving all the question in line so in 2-3 ques i needed dijkstra so i just kept it there ignore it. dm me your code.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by arham_doshi (previous revision, new revision, compare).

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

CAN SOMEONE PLEASE HELP ME???

for Investigation question above, i am doing exactly what the author does except for one thing:

he uses a visited array and if already visited, continues..

i do something like

pair = pq.top();
if(pair.first > distance[pair.second])
     continue;

i want to know constraint / testcase wise why this will give TLE. it was giving tle for 3 large testcases

the issue was resolved on introducing a boolean visited array

mycode = https://ideone.com/e6ZTWL (not needed tho)

»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

https://cses.fi/paste/cfd81e7f5b4db001e2466/

what is wrong in this solution? CycleFinding?????

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Not sure but may be possible that the distance you are initating with LLONG_MAX may get over fload when you do distance[i] + someting

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

can someone help me understanding this lines in Labyrinth problem?

p = from[p.x][p.y][0];

what does from means? i was unable to googling it. is there any keyword? thank you

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    It is just 2d vector, i have declared it on 3rd line, it helps us in recreating the path.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      what does FRM and DIR stand for? You mentioned it on the same Labyrinth problem. Thanks for this blog btw.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        FRM stands for FROM means from which cell it has come to this cell, and DIR stands for directon means from previous cell in which directon do we move (right, left, up, down) to reach current cell. Both these auxiliary array help us in recreating path after wards.

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          got it thanks, i did a similar thing but i thought FRM or DIR was some advanced thing that ive never heard of

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +5 Vote: I do not like it

      got it, thanks!

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Thanks for the editorial!!

Really great job!

Just want to add that in the problem Monsters, after applying multi source bfs we can use dfs as well instead of bfs to find the path from 'A' to boundary, as we need not find the shortest path.

My Implementation
  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Thanks dhruv, one more thing i noticed is that if you use dfs you don't need the auxiliary array to recreate path, nice one.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      what is the time complexity if we run BFS on multiple monsters? For each monster we might take n.m time right?

      then overall complexity will be too high?

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        no the time complexity will still be n*m , we will just start bfs with all monsers simalteniosly.

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Because we visit at most every node once (or five times, once when we start and once for each direction, but it's still constant).

  • »
    »
    3 years ago, # ^ |
    Rev. 4   Vote: I like it 0 Vote: I do not like it

    Hey RisingWizard arham_doshi Can you explain this line if(d[i][j+1]>ans.size()+1) ?

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

IN THE FLIGHT ROUTES PROBLEM if you want to find k routes each vertex is visited atmax k times in k routes. Can somebody explain me why it is so?

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

How to solve Planet Queries II ?

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Too much helpful. Thanks a lot.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In Highscores I dont understand how you detected positive cycle using dfs , can anyone please explain a bit more..

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    We first find positive cycles using Bellman-Ford, and then we check if these cycles can be reached on a path from 1->n. We can do this by reversing the edges and running a dfs from node n, and running a normal dfs from node 1.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone help me out in the implementation of Dijkstra's Algo in python using heaps, I'm getting TLE in 2 test cases only

My code

Thanks

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I suggest you to use c++ or java as the test cases in cses are very tight.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

arham_doshi I have a different approach for the problem "Flight Discount" But getting wrong answer on 3 TC can you help me please,I can't find any TC.

My Solution

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone help why this Java code gives TLE at test case #8 and how to optimize it?

Code

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by arham_doshi (previous revision, new revision, compare).

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I'm trying to solve Shortest Routes II using Dijkstra but it is giving me TLE on 5 cases. I used same approach for Shortest Routes I and it got accepted but for 2nd one it's not. I'm also taking care of sub-optimal cases by not inserting the already processed nodes still it's giving me TLE. Could anyone suggest how to optimize the Djikstra approach. My solution.

Any help would be appreciated. Thank you.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Since actually we need to know all distances from each vertex to each other vertex a solution based on Floyd-Warshall works more efficient here.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

While solving finding cycles using bellman ford, if I iterate over a vector<vector>edges, which is a list of edges, I get a TLE, but if I iterate over an adjacency list instead of that, it runs faster. Why is that?

Code with list of edges
Code with adjacency list
  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    instead of for(auto e: edges) use reference. for(auto &e: edges)

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Wow it worked!! But why is copying a small vector such an overhead I wonder? Thanks a lot though!

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Can somebody help me understand why is my code https://cses.fi/paste/dfd9fc301c9be88b1f65d5/ giving TLE in one of the test cases of Labyrinth, I did it very similar to editorail.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    After working on it for quite some time I found that instead of storing the answer in string , storing in stack passed that one test case which previously gave me TLE.

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In shortest Routes 2 we have used Floyd warshall algorithm which has a time complexity of O(n3) but if we use Dijkstra algorithm for every node to find the shortest path between it and other nodes its time complexity will be O(n2*log(n)) but still Dijkstra algorithm gives TLE while Floyd warshall algorithm doesn't can someone please explain where I am doing wrong

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    no u are wrong about the time complexity of dijkstra for a node it is (V+E*log(V)) where E is our no. of edges and V is no. of vertices or nodes so E in worst can be V^2 and applying for every node to find the shortest path between it and other nodes its time complexity will be O(V*E*log(V)+V*V) which in worst case can be V^3*log(V)

»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Can't we solve flight discounts using dp? I tried this approach but my soln is giving WA for 3 test cases. Like dp[n][2], where the dp[s][c] is the shortest path to s from 1 using the discount c times.

Code
»
23 months ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it

Editorial for Road Reparation

Ques link

First check graph is connected or not. If it is not connected then answer is not possible.

If it is connected find minimum spanning tree. You can use prism algo for finding MST.

My code link

»
23 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Editorial for Flight Route Check

Ques LinK

Run the dfs from any node and then check all nodes are visited or not. if any node is not visited, u will get the answer.

Also check is there any node is present in graph whose outdegree is zero. if yes then u can visited from this node to any node.

Else the answer is YES.

My Code Link

»
23 months ago, # |
  Vote: I like it 0 Vote: I do not like it

very useful

»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

In Shortest Routes 1, I am unable to understand why is it giving me wrong answers. Can some help me out pls? Thanks!

My Solution

  • »
    »
    11 months ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    Hey, I got the same issue and it seems that we both have a more optimal answer than the verdict answer as I see in the testcases, if you've figured out what was wrong please inform because I'm really stuck. Thanks in advance. Here is the code EDIT: it turns out that it is a directed graph lol what a mistake. Here is the accepted solution

»
12 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I tried solving 12. Finding Cycles using DFS (I know that Bellman–Ford exists, I just liked the idea of using prefix sums to track if a cycle is negative or not), but I keep getting TLE's in tests 7, 13 and 15 (the graph structure in those cases is something like a tree, I guess that's why this could be the case). Is this kind of problem solvable by DFS, or because it is a weighted directed graph (and I have to revisit the same node multiple times) the worst-case time complexity is worse than $$$O(n + m)$$$?

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone tell me why doing simple dijkstra on Longest Flight Routes fails TC#15?

Spoiler: Code
»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Round trip did not works using bfs.. don't know why.. if it works using bfs let me know please.

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

For the problem High Scores there is a simpler solution that is easier to implement. You can just use Bellman Ford to calculate the maximum distance from node 1 to n. Run the loop n — 1 times ( for calculating maximum distances using Bellman Ford ) and then scan the edges for one last time. If the distance to a node u seems to increase again, this indicates u is part of a positive weight cycle ( sum of weights in the cycle u that is part of > 0 ).

If that is the case, check if node n is reachable from u and print -1 if it is.

Link to my solution

  • »
    »
    8 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have an even simpler solution for High Scores: https://cses.fi/paste/ba556eee811dc61873945c/

    We can just run Bellman-Ford one more time, and check if the last guy changes. Whenever we change a node, we decrease both distances by a large amount (so that the updates are fast).

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Planet Queries I

An alternative to binary lifting. A natural solution is to exploit the properties of the graph. Each node has only 1 outgoing edge. Similarly, if there's a cycle then there's no outgoing edge, nodes can only teleport into the cycle. For nodes that aren't self loops, they are in a cycle or they will reach it after a certain number of jumps.

We find the strongly connected components in the graph. $$$O(n + m)$$$

Given that the condensation graph is topologically sorted, it is possible to have a constant time response per query. But queries need to be processed in the topological order (staring with big cycles, then self-loops, then first nodes that join cycles, etc.).

I got it to pass with C++, Python IO is unfortunately half of the exec time.