[Accepted] Swift
```
class Solution {
    func countOfPairs(_ n: Int, _ x: Int, _ y: Int) -> [Int] {

        let edges: [[Int]] = {
            var edges = Array(repeating: [Int](), count: n + 1)

            edges[1].append(2)
            edges[n].append(n - 1)

            if n > 2 {
                for i in 2...n - 1 {
                    edges[i].append(i - 1)
                    edges[i].append(i + 1)
                }
            }

            if x != y {
                edges[x].append(y)
                edges[y].append(x)
            }

            return edges
        }()

        func dfs(_ start: Int) -> [Int] {
            var res = Array(repeating: Int.max, count: n + 1)
            var stack = [start]

            res[start] = 0

            while !stack.isEmpty {
                var newStack = [Int]()

                for start in stack {
                    for next in edges[start]
                        where res[next] > res[start] + 1 {
                            res[next] = res[start] + 1
                            newStack.append(next)
                        }
                }

                stack = newStack
            }

            return res
        }

        let dists = (1...n).flatMap(dfs)

        return (1...n).map { k in dists.filter({ $0 == k }).count }
    }
}
```
['Swift']