D. Harmonious Graph
题目: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相关推荐
- 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 ...
- D. Harmonious Graph【并查集】
You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...
- Harmonious Graph (并查集 —父亲为最大值)
You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to n. The graph ...
- Harmonious Graph
You're given an undirected graph with nn nodes and mm edges. Nodes are numbered from 11 to nn. The g ...
- Harmonious Graph(并查集)
思路 题意:n个点,m条边,组成若干个连通图,如果一个点x能到另一个点y,那么如果(x,y)中任意一个点,都能到y,就称这个图为和谐图,求需要连多少个边,使得所有连通图成为和谐图 做法:用并查集存下所 ...
- CF 1253D-Harmonious Graph-并查集+贪心
Description You're given an undirected graph with n nodes and m edges. Nodes are numbered from 1 to ...
- 图融合GCN(Graph Convolutional Networks)
图融合GCN(Graph Convolutional Networks) 数据其实是图(graph),图在生活中无处不在,如社交网络,知识图谱,蛋白质结构等.本文介绍GNN(Graph Neural ...
- Graph Representation 图神经网络
Graph Representation 图神经网络 图表示学习(representation learning)--图神经网络框架,主要涉及PyG.DGL.Euler.NeuGraph和AliGra ...
- TVM 图优化Graph Optimization
TVM 图优化Graph Optimization Codegen
最新文章
- str_repeat() 函数
- Object.entries(obj)
- swift--添加新手引导页
- Java 位运算- 十进制数值转十六进制(内存中的数据)
- jquery里面的$(this)和this都什么时候用,有什么区别
- mybatis比hibernate处理速度快的原因
- hihocoder 1183 割点和割边
- leetcode1069. 产品销售分析 II(SQL)
- 解放双手!推荐一款阿里开源的低代码工具,YYDS!
- freemarker 导出html格式word_如何导出 Kindle 上的读书笔记
- Python_base_正则表达式
- linq 根据指定条件返回集合中不重复的元素
- mac简体拼音打出来是英文_mac怎样打出拼音 - 卡饭网
- 【Python】【pygame】更逼真的星星、连绵细雨
- 数据技术篇之数据同步
- error An unexpected error occurred: “EPERM: operation not permitted, unlink ‘C:\\Users
- unity零基础开始学习做游戏(四)biu~biu~biu发射子弹打飞机
- 超详细版-计算网络地址、子网、广播地址、主机数
- APP开放源码第一弹《纳豆》
- 万能码,你的启明星(安全扫码专业委员会)