题目大意:

n个点,m条边,两个数l和r,如果l和r相连接,那么对于l和r之间值任意一个数都要和l相连。问达到这一目的需要添加的边的最小数量。

题解:

我们首先要找到当前连通块中最大的那个点,也就是说所有小于当前点的点都要和这个点相连,如果不相连的话,加一条边,所以用我们可以用一个mark数组来标记当前当前点是否在当前联通块中,如不在的话,并且当前点的大小小于联通块的最大点的大小,我们就要加一条边。可以用dfs来遍历联通块

#include<bits/stdc++.h>
using namespace std;
const int N=2E5+;
vector<int >ve[N];
int mark[N];
int s=;
void dfs(int x){
mark[x]=;
s=max(s,x);
for(int i=;i<ve[x].size();i++){
int a=ve[x][i];
if(!mark[a]) dfs(a);
}
}
int main(){
int n,m;
cin>>n>>m;
int x,y;
for(int i=;i<=m;i++) {
cin>>x>>y;
ve[x].push_back(y);
ve[y].push_back(x);
}
int ans=;
for(int i=;i<=n;i++){
if(mark[i]) continue ;
if(i<s){
ve[i].push_back(s);
ve[s].push_back(i);
ans++;
}
dfs(i);
}
cout<<ans<<endl;
return ;
}

太秒啦!!!!

D - Harmonious Graph的更多相关文章

  1. Harmonious Graph

    D. Harmonious Graph 好后悔在写这个题之前浪费了几分钟时间,不然我就写出来了.... 因为他就是连通块之间的合并问题,所以就用并查集就好了 复杂度好像也只是线性的吧... 然后就A了 ...

  2. Codeforces Round #600 (Div. 2) D。 Harmonious Graph

    #include<iostream> using namespace std ; ; int p[N]; int cnt; int find(int x) { if(p[x]!=x) p[ ...

  3. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

  4. Codeforces Round #600 (Div. 2)

    传送门 A. Single Push 直接乱搞即可. Code /* * Author: heyuhhh * Created Time: 2019/11/16 22:36:20 */ #include ...

  5. CF Round #600 (Div 2) 解题报告(A~E)

    CF Round #600 (Div 2) 解题报告(A~E) A:Single Push 采用差分的思想,让\(b-a=c\),然后观察\(c\)序列是不是一个满足要求的序列 #include< ...

  6. [开发笔记] Graph Databases on developing

    TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...

  7. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  8. POJ 2125 Destroying the Graph 二分图最小点权覆盖

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8198   Accepted: 2 ...

  9. [LeetCode] Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

随机推荐

  1. 改变Dataframe的列的数据类型

    1.查看DataFrame的数据类型 df.dtypes#查看各列数据类型 df[A].dtypes#查看A列数据类型 2.转换DataFrame的数据类型 df[A].astypes(int)#将A ...

  2. Building Applications with Force.com and VisualForce (DEV401) (二三):Visualforce Componets (Tags) Library Part III

    Dev401-024:Visualforce Pages: Visualforce Componets (Tags) Library Part IIIStatic Resources1.Static ...

  3. 10行Python代码实现目标检测

    要知道图像中的目标是什么? 或者你想数一幅图里有多少个苹果? 在本文中,我将向你展示如何使用Python在不到10行代码中创建自己的目标检测程序. 如果尚未安装python库,你需要安装以下pytho ...

  4. 编译原理:非确定的自动机NFA确定化为DFA

    1.设有 NFA M=( {0,1,2,3}, {a,b},f,0,{3} ),其中 f(0,a)={0,1}  f(0,b)={0}  f(1,b)={2}  f(2,b)={3} 画出状态转换矩阵 ...

  5. 线程间交换数据的Exchanger

    作者:Steven1997 链接:https://www.jianshu.com/p/9b59829fb191 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处. Exc ...

  6. html前端之css基础

    CSS 属性导航: CSS 属性组 动画 背景 边框和轮廓 框 颜色 内容页的媒体属性 尺寸 盒子模型(新) 盒子模型(旧) 字体 内容生成 网格 超链接 线框 列表 外边距 字幕 多列 内边距 页面 ...

  7. Spring Web Flow 笔记

    在Spring 中配置 Web Flow <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...

  8. Golang入门(2):一天学完GO的基本语法

    摘要 在配置好环境之后,要研究的就是这个语言的语法了.在这篇文章中,作者希望可以简单的介绍一下Golang的各种语法,并与C和Java作一些简单的对比以加深记忆.因为这篇文章只是入门Golang的第二 ...

  9. 微信号网页版api

    Django Wechat Api djangowechatapi是基于wxpy和django制作的web应用 安装 使用pip pip install djangowechatapi 源码安装 gi ...

  10. postman 工具接口测试

    一.get:请求多个参数时,需要用&连接 eg:http://api.***.cn/api/user/stu_info?stu_name=小黑&set=女   eg:接口请求参数放在b ...