Блог пользователя samudra_mitra

Автор samudra_mitra, история, 3 года назад, По-английски

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!!

  • Проголосовать: нравится
  • +68
  • Проголосовать: не нравится

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
3 года назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

Sugoi! ^⁠_⁠^

»
3 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Thanks 。⁠◕⁠‿⁠◕⁠。

»
3 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

cool

»
3 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Great job!

»
3 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

Nicely compiled, thanks

»
3 года назад, скрыть # |
Rev. 3  
Проголосовать: нравится +1 Проголосовать: не нравится

It is a helpful initiative,thanks. :)

»
3 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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 :)

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
23 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

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

»
20 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
17 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

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
»
15 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For Flight discount we can use much simpler method for solving:

Hint
Solution Idea
AC code
»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

why is this code wrong, it prints the cycle in another order, but it should pass i think, as the question says you can print any cycle. https://cses.fi/paste/71f1ae23b2ad8acdcd1093/

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hello. I think there is a mistake in the explanation of Cycle Finding problem. It looks like you are taking only vertex $$$1$$$ as the source, but the graph can be disconnected and there can be negative cycles not reachable from vertex $$$1$$$. To consider all those, we have to find a source for each component and to do so, I think finding topo sort and running bfs in topo sort order would work.

Also, your code for the same problem fails on all testcases. Please look into it.

  • »
    »
    10 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    I have updated the code.. thanks for noticing the error

  • »
    »
    8 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    A simpler approach is to maintain a visited array. Run the Bellman-Ford algorithm starting from every unvisited node. During each iteration, mark all visited vertices as true. If no updates are made in an iteration, you can terminate early. If any vertex remains unvisited after the process, run Bellman-Ford again using it as the new source. However, if updates still occur during the n-th iteration, it indicates the presence of a negative-weight cycle — in that case, trace and report the cycle.

»
10 месяцев назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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