A maze consists of n rooms numbered from 1 to n, and some rooms are connected by corridors. You are given a 2D integer array corridors where corridors[i] = [room1i, room2i] indicates that there is a corridor connecting room1i and room2i, allowing a person in the maze to go from room1i to room2iand vice versa.
The designer of the maze wants to know how confusing the maze is. The confusionscore of the maze is the number of different cycles of length 3.
For example, 1 -> 2 -> 3 -> 1 is a cycle of length 3, but 1 -> 2 -> 3 -> 4 and 1 -> 2 -> 3 -> 2 -> 1 are not.
Two cycles are considered to be different if one or more of the rooms visited in the first cycle is not in the second cycle.
Input: n =5, corridors =[[1,2],[5,2],[4,1],[2,4],[3,1],[3,4]]Output: 2Explanation:
One cycle of length 3is4->1->3->4, denoted in red.Note that thisis the same cycle as 3->4->1->3 or 1->3->4->1 because the rooms are the same.Another cycle of length 3is1->2->4->1, denoted in blue.Thus, there are two different cycles of length 3.
Example 2:
1
2
3
4
Input: n =4, corridors =[[1,2],[3,4]]Output: 0Explanation:
There are no cycles of length 3.
We need to count the number of unique cycles of length 3 (triangles) in an undirected graph. For each pair of connected nodes, count the number of common neighbors. Each triangle is counted once per edge when we sum common neighbors across edges; since each triangle has 3 edges, it will be counted 3 times in total, so divide the final sum by 3 to get the number of distinct triangles.
Build an adjacency set for each node. For each edge (u, v), count the number of common neighbors between u and v (this equals the number of triangles that include edge (u, v)). Sum this for all edges, then divide the total by 3 to obtain the number of unique length-3 cycles.
#include<vector>#include<unordered_set>usingnamespace std;
classSolution {
public:int numberOfPaths(int n, vector<vector<int>>& corridors) {
vector<unordered_set<int>> g(n+1);
for (auto& e : corridors) {
g[e[0]].insert(e[1]);
g[e[1]].insert(e[0]);
}
int cnt =0;
for (int u =1; u <= n; ++u) {
for (int v : g[u]) if (u < v) {
for (int w : g[u]) if (w > u && w != v && g[v].count(w)) cnt++;
}
}
return cnt /3;
}
};
import java.util.*;
classSolution {
publicintnumberOfPaths(int n, int[][] corridors) {
List<Set<Integer>> g =new ArrayList<>();
for (int i = 0; i <= n; i++) g.add(new HashSet<>());
for (int[] e : corridors) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
int cnt = 0;
for (int u = 1; u <= n; u++) {
for (int v : g.get(u)) if (u < v) {
for (int w : g.get(u)) if (w > u && w != v && g.get(v).contains(w)) cnt++;
}
}
return cnt / 3;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funnumberOfPaths(n: Int, corridors: Array<IntArray>): Int {
val g = Array(n+1) { mutableSetOf<Int>() }
for (e in corridors) {
g[e[0]].add(e[1])
g[e[1]].add(e[0])
}
var cnt = 0for (u in1..n) {
for (v in g[u]) if (u < v) {
for (w in g[u]) if (w > u && w != v && w in g[v]) cnt++ }
}
return cnt / 3 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
defnumberOfPaths(n: int, corridors: list[list[int]]) -> int:
from collections import defaultdict
g = defaultdict(set)
for u, v in corridors:
g[u].add(v)
g[v].add(u)
cnt =0for u in range(1, n+1):
for v in g[u]:
if u < v:
for w in g[u]:
if w > u and w != v and w in g[v]:
cnt +=1return cnt //3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::collections::HashSet;
fnnumber_of_paths(n: usize, corridors: Vec<Vec<usize>>) -> i32 {
letmut g =vec![HashSet::new(); n+1];
for e in corridors.iter() {
g[e[0]].insert(e[1]);
g[e[1]].insert(e[0]);
}
letmut cnt =0;
for u in1..=n {
for&v in&g[u] {
if u < v {
for&w in&g[u] {
if w > u && w != v && g[v].contains(&w) { cnt +=1; }
}
}
}
}
cnt /3}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
functionnumberOfPaths(n: number, corridors: number[][]):number {
constg: Set<number>[] = Array.from({length: n+1}, () =>newSet());
for (const [u, v] ofcorridors) {
g[u].add(v);
g[v].add(u);
}
letcnt=0;
for (letu=1; u<=n; u++) {
for (constvofg[u]) if (u<v) {
for (constwofg[u]) if (w>u&&w!==v&&g[v].has(w)) cnt++;
}
}
return Math.floor(cnt/3);
}
⏰ Time complexity:O(M * D) – For each of the M edges we iterate over the neighbors of one endpoint (average degree D) and check membership in the other’s adjacency set.
🧺 Space complexity:O(N + M) – We store adjacency sets for all nodes; in the worst case of a dense graph this can be O(N^2).