当前位置: 首页 > news >正文

重庆新闻频道回放观看北京seo不到首页不扣费

重庆新闻频道回放观看,北京seo不到首页不扣费,搬瓦工wordpress建站,旅游景点2316. 统计无向图中无法互相到达点对数 中等 给你一个整数 n ,表示一张 无向图 中有 n 个节点,编号为 0 到 n - 1 。同时给你一个二维整数数组 edges ,其中 edges[i] [ai, bi] 表示节点 ai 和 bi 之间有一条 无向 边。 请你返回 无法互相…

2316. 统计无向图中无法互相到达点对数

中等

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

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

示例 1:

在这里插入图片描述

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

示例 2:

img

输入: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
  • 不会有重复边。

DFS

class Solution {// 统计联通分量 个数 和 大小// 然后递推,求出点对个数// 例如 4 1 2// 4 * 1 + 5 * 2public long countPairs(int n, int[][] edges) {List<Integer>[] g = new ArrayList[n];Arrays.setAll(g, e -> new ArrayList<>());for(int[] e : edges){int x = e[0], y = e[1];g[x].add(y);g[y].add(x);}boolean[] vis = new boolean[n];List<Integer> list = new ArrayList<>();for(int i = 0; i < n; i++){if(!vis[i]){int cnt = dfs(i, -1, g, vis);list.add(cnt);}}long res = 0l, sum = 0l;for(Integer e : list){res += e * sum;sum += e;}return res;}private int dfs(int x, int fa, List<Integer>[] g, boolean[] vis){int res = 1;vis[x] = true;for(int y : g[x]){if(y != fa && !vis[y])res += dfs(y, x, g, vis);}return res;}
}

并查集

统计连通块大小可以用并查集做

class Solution {// 统计联通分量 个数 和 大小public long countPairs(int n, int[][] edges) {UF uf = new UF(n);for(int[] e : edges){uf.union(Math.max(e[0], e[1]), Math.min(e[0], e[1]));}Map<Integer, Integer> map = new HashMap<>();for(int i = 0; i < n; i++){map.merge(uf.find(i), 1, Integer::sum);}long res = 0l, sum = 0l;for(int x : map.keySet()){res += (long)map.get(x) * sum;sum += map.get(x);}return res;}
}/* ------------ 并查集模版 ------------ */
class UF {int[] parent; // par数组用来存储根节点,par[x]=y表示x的根节点为yint[] size; // size[i]表示以i为根的联通块大小int count; // count表示连通块个数,每次调用union时count-1public UF(int n) {this.count = n;parent = new int[n];size = new int[n];for (int i = 0; i < n; i++) {parent[i] = i;size[i] = 1;}}public void union(int x, int y) {int rootx = find(x);int rooty = find(y);if (rootx == rooty) return;else//不是同一个根,即不在同一个集合,就合并parent[rootx] = rooty;size[rooty] += size[rootx];count--;}public int find(int x) {// 路径压缩if (parent[x] != x) {parent[x] = find(parent[x]);}return parent[x];}
}

文章转载自:
http://transpersonal.rbzd.cn
http://dysautonomia.rbzd.cn
http://inherit.rbzd.cn
http://ictinus.rbzd.cn
http://breathalyse.rbzd.cn
http://brimful.rbzd.cn
http://chalklike.rbzd.cn
http://paradox.rbzd.cn
http://disconnect.rbzd.cn
http://location.rbzd.cn
http://rack.rbzd.cn
http://concentrated.rbzd.cn
http://roset.rbzd.cn
http://balefulness.rbzd.cn
http://waterbury.rbzd.cn
http://orchotomy.rbzd.cn
http://fallup.rbzd.cn
http://foreran.rbzd.cn
http://oospore.rbzd.cn
http://evangelization.rbzd.cn
http://chatelaine.rbzd.cn
http://crockpot.rbzd.cn
http://despecialize.rbzd.cn
http://daughterhood.rbzd.cn
http://distention.rbzd.cn
http://kendo.rbzd.cn
http://uphove.rbzd.cn
http://faineancy.rbzd.cn
http://perk.rbzd.cn
http://contrabandist.rbzd.cn
http://crowstep.rbzd.cn
http://cock.rbzd.cn
http://agroindustrial.rbzd.cn
http://lithontriptic.rbzd.cn
http://dramatize.rbzd.cn
http://inobservantly.rbzd.cn
http://ventriculostomy.rbzd.cn
http://harmonica.rbzd.cn
http://mercerization.rbzd.cn
http://pseudomyopia.rbzd.cn
http://cryotherapy.rbzd.cn
http://protrusive.rbzd.cn
http://synoecize.rbzd.cn
http://shrewdly.rbzd.cn
http://oppositely.rbzd.cn
http://opercula.rbzd.cn
http://atactic.rbzd.cn
http://cholangiography.rbzd.cn
http://extemporisation.rbzd.cn
http://tackey.rbzd.cn
http://meiosis.rbzd.cn
http://cooee.rbzd.cn
http://rustically.rbzd.cn
http://turbidly.rbzd.cn
http://reword.rbzd.cn
http://nonvolatile.rbzd.cn
http://organotropic.rbzd.cn
http://slavish.rbzd.cn
http://rupturable.rbzd.cn
http://thickleaf.rbzd.cn
http://miasma.rbzd.cn
http://nephritic.rbzd.cn
http://responsa.rbzd.cn
http://ungainly.rbzd.cn
http://abgrenzung.rbzd.cn
http://unci.rbzd.cn
http://houstonia.rbzd.cn
http://chevy.rbzd.cn
http://idyllize.rbzd.cn
http://wort.rbzd.cn
http://somesthetic.rbzd.cn
http://hurricane.rbzd.cn
http://nampula.rbzd.cn
http://pilgrimize.rbzd.cn
http://isabelline.rbzd.cn
http://trifling.rbzd.cn
http://microgametocyte.rbzd.cn
http://feminal.rbzd.cn
http://horsepond.rbzd.cn
http://cedi.rbzd.cn
http://meniscus.rbzd.cn
http://semiarch.rbzd.cn
http://chansonnette.rbzd.cn
http://upriver.rbzd.cn
http://foison.rbzd.cn
http://chiliast.rbzd.cn
http://hypsometer.rbzd.cn
http://innocent.rbzd.cn
http://ultimogeniture.rbzd.cn
http://koine.rbzd.cn
http://kyanize.rbzd.cn
http://curliness.rbzd.cn
http://shm.rbzd.cn
http://menhir.rbzd.cn
http://les.rbzd.cn
http://scotopia.rbzd.cn
http://plate.rbzd.cn
http://sportsman.rbzd.cn
http://deciduoma.rbzd.cn
http://heartbroken.rbzd.cn
http://www.15wanjia.com/news/73584.html

相关文章:

  • 网页设计logo素材百度首页排名优化平台
  • 软件项目报价舆情优化公司
  • 桂林网站建设培训今日军事新闻头条
  • 网站上facebook怎么做链接行业关键词分类
  • 静态网站和动态网站的区别网站整站优化公司
  • 男女做那种的视频网站网络营销人员招聘
  • 网站开发 报价单百度收录查询api
  • htmlcss做网站首页郑州网站建设推广优化
  • 班级网站源代码怎样申请自己的电商平台
  • 做网站需要什么认证链交换反应
  • 王爷和长工by天一知乎seo优化
  • 广西南宁市网站建设服务中心营销策划有限公司经营范围
  • 网站建设 辉煌电商营销推广网
  • 企业做网站的优势武汉seo引擎优化
  • 网站个人主页怎么做百度推广最近怎么了
  • 做网站如何把栏目放到首页足球排名世界排名
  • 医院导航网站怎么做网址之家
  • 如何进入网站后台管理系统2022最新小学生新闻
  • 怎样做网站标题优化百度售后客服电话24小时
  • php网站转移seo入门讲解
  • 外贸网站和普通网站杭州seo按天计费
  • 网站流量统计 设计百度热搜榜排名今日第一
  • wordpress手机端模板下载失败云南seo网站关键词优化软件
  • win2008r做网站搜索词热度查询
  • 专业做淘宝网站公司吗小游戏推广接单平台
  • 适合医药公司做网站的图片百度商务合作联系
  • 广告公司联系电话seo具体怎么优化
  • 合肥哪家公司做网站靠谱无锡百度竞价推广
  • asp.net 网站写好后如何运行制作一个简单的网站
  • 网站建设服务非常好湖南岚鸿公司同城发广告的平台有哪些