Advanced Graphs
n people labelled from 1 to n. There is a rumour that one of these people is secretly the town judge. If the town judge exists, then:
trust where trust[i] = [ai, bi] representing that the person labelled ai trusts the person labelled bi. If a trust relationship does not exist in trust array, then such a trust relationship does not exist.
-1 otherwise.
This is a directed graph problem. Each trust relationship [a, b] is a directed edge from a to b. The town judge is the person who has zero outgoing edges and exactly n - 1 incoming edges.
You do not need to build a full graph for this one. Instead, keep a count for each person. For every trust edge [a, b], decrement the count for a (they trust someone, so they cannot be the judge) and increment the count for b (they are trusted by someone). The judge is the person whose count equals n - 1.
count of size n + 1 initialised to zero (1-indexed)[a, b] in trust: do count[a] -= 1 and count[b] += 1i where count[i] == n - 1-1n - 1 others (incremented n - 1 times). Anyone who trusts someone gets decremented at least once, so their count can never reach n - 1.n vertices, where each vertex is labelled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where each edges[i] = [ui, vi] denotes a bi-directional edge between vertex ui and vertex vi. Every vertex pair is connected by at most one edge, and no vertex has an edge to itself.
source to vertex destination.
edges and the integers n, source, and destination, return true if there is a valid path from source to destination, or false otherwise.
This is a standard graph reachability problem. You have solved similar problems before in earlier weeks. Think about which graph traversal technique you would reach for to check whether two nodes are connected.
Either BFS or DFS works perfectly here. Build an adjacency list from the edge list, then start a traversal from source. If you reach destination at any point, return true. If the traversal finishes without visiting it, return false.
[u, v], add v to adj[u] and u to adj[v]visited set, initialise a queue (BFS) or stack (DFS) with sourcedestination, return true. Otherwise mark it visited and enqueue all unvisited neighboursdestination, return falsesource and destination belong to the same group. BFS/DFS is more straightforward here though.-1. Nodes are numbered consecutively from 1 to n, and edges have varying weights.
n: the number of nodes in the graphedges: a 2D array where each element [u, v, w] describes an undirected edge between nodes u and v with weight ws: the starting nodes to each other node in ascending order of node label, excluding s itself. Use -1 for unreachable nodes.
This is the only hard problem in this week. It is a straightforward application of Dijkstra's algorithm, but two things to watch out for: the vertex numbers are 1-based rather than 0-based, and the function must return the distance to every node excluding the start node. Unreachable nodes should have a distance of -1.
Dijkstra's algorithm finds the shortest path from a single source to all other vertices in a weighted graph with non-negative edge weights. Use a priority queue (min-heap) to always process the closest unvisited node next.
There can be multiple edges between the same pair of nodes with different weights. Keep all of them; Dijkstra will naturally pick the shortest route.
dist array of size n + 1 with infinity for every node, except dist[s] = 0(0, s) onto a min-heap(d, u). If d > dist[u], skip it (stale entry). Otherwise, for each neighbour (v, w) of u, if dist[u] + w < dist[v], update dist[v] and push (dist[v], v)dist[i] for all i from 1 to n, skipping s. Replace any remaining infinity values with -1