Skip to content

Latest commit

 

History

History
222 lines (181 loc) · 5.44 KB

File metadata and controls

222 lines (181 loc) · 5.44 KB

English Version

题目描述

给你一个整数 n ,表示一张 无向图 中有 n 个节点,编号为 0 到 n - 1 。同时给你一个二维整数数组 edges ,其中 edges[i] = [ai, bi] 表示节点 ai 和 bi 之间有一条 无向 边。

请你返回 无法互相到达 的不同 点对数目 。

 

示例 1:

输入:n = 3, edges = [[0,1],[0,2],[1,2]]
输出:0
解释:所有点都能互相到达,意味着没有点对无法互相到达,所以我们返回 0 。

示例 2:

输入:n = 7, edges = [[0,2],[0,5],[2,4],[1,6],[5,4]]
输出:14
解释:总共有 14 个点对互相无法到达:
[[0,1],[0,3],[0,6],[1,2],[1,3],[1,4],[1,5],[2,3],[2,6],[3,4],[3,5],[3,6],[4,6],[5,6]]
所以我们返回 14 。

 

提示:

  • 1 <= n <= 105
  • 0 <= edges.length <= 2 * 105
  • edges[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • 不会有重复边。

解法

Python3

class Solution:
    def countPairs(self, n: int, edges: List[List[int]]) -> int:
        def dfs(i):
            vis[i] = True
            res = 1
            for j in g[i]:
                if not vis[j]:
                    res += dfs(j)
            return res

        g = defaultdict(list)
        for a, b in edges:
            g[a].append(b)
            g[b].append(a)
        vis = [False] * n
        arr = []
        for i in range(n):
            if not vis[i]:
                arr.append(dfs(i))
        ans = t = 0
        for v in arr:
            t += v
            ans += v * (n - t)
        return ans

Java

class Solution {
    private boolean[] vis;
    private List<Integer>[] g;

    public long countPairs(int n, int[][] edges) {
        vis = new boolean[n];
        g = new List[n];
        for (int i = 0; i < n; ++i) {
            g[i] = new ArrayList<>();
        }
        for (int[] e : edges) {
            int a = e[0], b = e[1];
            g[a].add(b);
            g[b].add(a);
        }
        List<Integer> arr = new ArrayList<>();
        for (int i = 0; i < n; ++i) {
            if (!vis[i]) {
                arr.add(dfs(i));
            }
        }
        int t = 0;
        long ans = 0;
        for (int v : arr) {
            t += v;
            ans += (long) v * (n - t);
        }
        return ans;
    }

    private int dfs(int i) {
        vis[i] = true;
        int res = 1;
        for (int j : g[i]) {
            if (!vis[j]) {
                res += dfs(j);
            }
        }
        return res;
    }
}

C++

class Solution {
public:
    vector<vector<int>> g;
    vector<bool> vis;

    long long countPairs(int n, vector<vector<int>>& edges) {
        vis.resize(n);
        g.resize(n, vector<int>());
        for (auto& e : edges) {
            int a = e[0], b = e[1];
            g[a].push_back(b);
            g[b].push_back(a);
        }
        vector<int> arr;
        for (int i = 0; i < n; ++i)
            if (!vis[i]) arr.push_back(dfs(i));
        long long ans = 0;
        int t = 0;
        for (int& v : arr) {
            t += v;
            ans += 1ll * v * (n - t);
        }
        return ans;
    }

    int dfs(int i) {
        int res = 1;
        vis[i] = true;
        for (int j : g[i])
            if (!vis[j]) res += dfs(j);
        return res;
    }
};

Go

func countPairs(n int, edges [][]int) int64 {
	vis := make([]bool, n)
	g := make([][]int, n)
	for _, e := range edges {
		a, b := e[0], e[1]
		g[a] = append(g[a], b)
		g[b] = append(g[b], a)
	}
	var arr []int
	var dfs func(int) int
	dfs = func(i int) int {
		res := 1
		vis[i] = true
		for _, j := range g[i] {
			if !vis[j] {
				res += dfs(j)
			}
		}
		return res
	}

	for i := 0; i < n; i++ {
		if !vis[i] {
			arr = append(arr, dfs(i))
		}
	}
	ans := 0
	t := 0
	for _, v := range arr {
		t += v
		ans += v * (n - t)
	}
	return int64(ans)
}

TypeScript

...