题目:D. Harmonious Graph

总结:将每一个连通块的所有点都指向该连通图的最大点。从1点开始遍历,假设fa[1] = x;那么从2~(x-1)的所有点的根结点都一定会指向x。(是在一个连通块),如果此时有一个点的根结点是y,不是x,那么就需要把这两个连通块连起来了。这时候从2开始到max(x,y),都一定是的根结点一定都是指向max(x,y),所以x的值要选择大的。避免两个连通块有重复的。此时就是和别的连通块做比较了。

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;int fa[N],n,m;int Find(int x){if(fa[x] == x) return x;fa[x] = Find(fa[x]);return fa[x];
}
int main(){scanf("%d%d",&n,&m);for(int i = 1;i <= n;i++) fa[i] = i;while(m--){int x,y,fx,fy;scanf("%d%d",&x,&y);fx = Find(x);fy = Find(y);//   printf("%d %d\n",fx,fy);if(fx > fy) swap(fx,fy);fa[fx] = fy;//大的做父亲结点}int ans = 0;for(int i = 1;i <= n;i++){int x = Find(i);while(i < x){int y  = Find(i);//  printf("%d %d %d\n",i,y,x);if(x != y){ans++;//为什么要把x变大呢,如果两个连通块通过一条线已经连起来。那么这两个连通块的根节点最大,否则会有重复的if(x < y) swap(x,y);fa[y] = x;}i++;}}printf("%d\n",ans);return 0;
}

D. Harmonious Graph相关推荐

  1. Harmonious Graph CodeForces - 1253D(思维+并查集)

    You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...

  2. D. Harmonious Graph【并查集】

    You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...

  3. Harmonious Graph (并查集 —父亲为最大值)

    You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...

  4. Harmonious Graph

    You're given an undirected graph with nn nodes and mm edges. Nodes are numbered from 11 to nn. The g ...

  5. Harmonious Graph(并查集)

    思路 题意:n个点,m条边,组成若干个连通图,如果一个点x能到另一个点y,那么如果(x,y)中任意一个点,都能到y,就称这个图为和谐图,求需要连多少个边,使得所有连通图成为和谐图 做法:用并查集存下所 ...

  6. CF 1253D-Harmonious Graph-并查集+贪心

    Description You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to ...

  7. 图融合GCN(Graph Convolutional Networks)

    图融合GCN(Graph Convolutional Networks) 数据其实是图(graph),图在生活中无处不在,如社交网络,知识图谱,蛋白质结构等.本文介绍GNN(Graph Neural ...

  8. Graph Representation 图神经网络

    Graph Representation 图神经网络 图表示学习(representation learning)--图神经网络框架,主要涉及PyG.DGL.Euler.NeuGraph和AliGra ...

  9. TVM 图优化Graph Optimization

    TVM 图优化Graph Optimization Codegen

最新文章

  1. str_repeat() 函数
  2. Object.entries(obj)
  3. swift--添加新手引导页
  4. Java 位运算- 十进制数值转十六进制(内存中的数据)
  5. jquery里面的$(this)和this都什么时候用,有什么区别
  6. mybatis比hibernate处理速度快的原因
  7. hihocoder 1183 割点和割边
  8. leetcode1069. 产品销售分析 II(SQL)
  9. 解放双手!推荐一款阿里开源的低代码工具,YYDS!
  10. freemarker 导出html格式word_如何导出 Kindle 上的读书笔记
  11. Python_base_正则表达式
  12. linq 根据指定条件返回集合中不重复的元素
  13. mac简体拼音打出来是英文_mac怎样打出拼音 - 卡饭网
  14. 【Python】【pygame】更逼真的星星、连绵细雨
  15. 数据技术篇之数据同步
  16. error An unexpected error occurred: “EPERM: operation not permitted, unlink ‘C:\\Users
  17. unity零基础开始学习做游戏(四)biu~biu~biu发射子弹打飞机
  18. 超详细版-计算网络地址、子网、广播地址、主机数
  19. APP开放源码第一弹《纳豆》
  20. 万能码,你的启明星(安全扫码专业委员会)

热门文章

  1. 银河linux看版本,下载Linux客户端 - 银河云盘 - 银河证券
  2. IDEA自动补全tab键向下选择s-tab向上选择
  3. 深圳计算机专业中专学校,深圳中专学校
  4. beeline 查询表数据导出到本地文件csv
  5. VMware虚拟机+Kali linux 2021.2 下载和安装以及初始操作
  6. CSS实现当鼠标移入或者移出时实现动画过渡效果
  7. 植树节汇报之后的突发奇想
  8. 一个计算机专业学生几年的Java编程经验汇总
  9. 自家小程序 如何实现定点跳转其他(比如:京东,天猫,淘宝)的小程序的具体店铺
  10. 2019年6月9日更正