samudra_mitra's blog

By samudra_mitra, history, 17 months ago, In English

Hello Codeforces, this is my first blog and here I have given my solution of the complete CSES Graph Algorithms section. This is the github repo where I have pushed all the cpp files:

Counting Rooms

Thinking
Code

Labyrinth

Thinking
Code

Building Roads

Thinking
Code

Message Route

Thinking
Code

Building Teams

Thinking
Code

Round Trip

Thinking
Code

Monsters

Thinking
Code

Shortest Routes I

Thinking
Code

Shortest Routes II

Thinking
Code

High Score

Thinking
Code

Flight Discount

Thinking
Code

Cycle Finding

Thinking
Code

Flight Routes

Thinking
Code

Round Trip II

Thinking
Code

Course Schedule

Thinking
Code

Longest Flight Route

Thinking
Code

Game Routes

Thinking
Code

Investigation

Thinking
Code

Planet Queries I

Thinking
Code

Planet Queries II

Thinking
Code

Planet Cycles

Thinking
Code

Road Reparation

Thinking
Code

Road Construction

Thinking
Code

Flight Routes Check

Thinking
Code

Planets and Kingdoms

Thinking
Code

Giant Pizza

Thinking
Code

Coin Collector

Thinking
Code

Mail Delivery

Thinking
Code

De Bruijn Sequence

Thinking
Code

Teleporters Path

Thinking
Code

Hamiltonian Flights

Thinking
Code

Knight's Tour

Thinking
Code

Download Speed

Thinking
Code

Police Chase

Thinking
Code

School Dance

Thinking
Code

Distinct Routes

Thinking
Code

I hope that you will find these helpful, and will also find it in your hearts to forgive any mistake which might have crept in unknowingly. Also, any feedback is welcome.

Peace!!

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

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

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

»
17 months ago, # |
  Vote: I like it +2 Vote: I do not like it

Sugoi! ^⁠_⁠^

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

Thanks 。⁠◕⁠‿⁠◕⁠。

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

cool

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

Great job!

»
17 months ago, # |
Rev. 3   Vote: I like it +1 Vote: I do not like it

Nicely compiled, thanks

»
17 months ago, # |
Rev. 3   Vote: I like it +1 Vote: I do not like it

It is a helpful initiative,thanks. :)

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

Good job!, I found them very helpful in my learning of Graph algorithms. Can you please update it to detailed version, because I can't understand some of them fully.

Sorry for bad english :)

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

Refer this video for easy implementation of round trip problem:-https://www.youtube.com/watch?v=KSEZ8BbIyHs&t=2s

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

I think there's a mistake on building teams you said you used bfs but in the code you used dfs.

»
7 months ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

In the problem Mail Delivery the stack part can be replaced with the following recursive code --

void dfs(int root){
	while(adj[root].size()){
		int m = adj[root].size();
		int it = *adj[root].begin();

		adj[root].erase(it);
		adj[it].erase(root);
		dfs(it);
	}

	path.pb(root);
}

The logic is the same as the stack method. Complete Code — https://ideone.com/FBA8MQ

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

Please include more detailed explanation.

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

In your solution for Download Speed in the adjacency matrix creation loop:

for (ll i = 0; i < m; i++)
    {
        ll x, y, w;
        cin >> x >> y >> w;
        if (adj[x][y] == -1)
        {
            adj[x][y] = 0;
        }
        adj[x][y] += w;
        adj[y][x] = 0; // THIS LINE GIVES ERROR FOR SOME CASES
        sum += w;
    }

There can be cases where the reverse edge(y->x) is also given in the input and this line will make the edgeweight of other edge(x->y) zero even if it was non-zero before. There are 3 test-cases #16, #17 and #18 which give wrong answer because of this.

It should be changed to :

if(adj[y][x]==-1) adj[y][x] = 0; 
//This ensure edge is only changed if it was initially absent

This passed all the test cases

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

This is an easier implementation of High_Score : The idea is to find out which nodes can reach the last node through any paths possible. If you have a positive length cycle with any of it's reaching nth node through any path then we can generate arbitrarily large number if this cycle is also connected to 1st node.

Implementation: create an edge list plus an adjacency matrix storing edges in reverse order , then run a dfs from nth node and mark the visited nodes, these are the nodes that can visit nth node.

Now run the bellman ford algorithm, on the nth iteration if any node which can reach nth node and its distance is changed that means the ans is -1.

Implementation
»
13 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Amazing, thanks a lot for this compilation.

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

For finding cyles why can we have single bellman ford iteration like this https://cp-algorithms.com/graph/finding-negative-cycle-in-graph.html