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 6 times (for each permutation of its 3 nodes), so divide the total by 6.
Build an adjacency set for each node. For each edge (u, v), count the number of common neighbors between u and v. Sum this for all edges, then divide by 6.
#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;
}
}
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 }
}
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
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}