题目大意:

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. Partition Array into Disjoint Intervals

    2020-02-10 22:16:50 问题描述: 问题求解: 解法一:MultiSet O(nlog) 看了下数据规模,第一个想到的是multiset,肯定可以ac的,就直接敲了出来. public ...

  2. 动态网站项目(Dynamic Web Project)登录功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8)(js验证+cookie)

    1.index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" p ...

  3. 【Unity游戏开发】跟着马三一起魔改LitJson

    一.引子 在游戏开发中,我们少不了和数据打交道,数据的存储格式可谓是百花齐放,xml.json.csv.bin等等应有尽有.在这其中Json以其小巧轻便.可读性强.兼容性好等优点受到广大程序员的喜爱. ...

  4. TensorFlow 实战卷积神经网络之 LeNet

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! LeNet 项目简介 1994 年深度学习三巨头之一的 Yan L ...

  5. 【Pytest04】全网最全最新的Pytest框架fixture应用篇(2)

    一.Fixture参数之params参数可实现参数化:(可以为list和tuple,或者字典列表,字典元祖等) 实例如下: import pytest def read_yaml(): '] @pyt ...

  6. js数据类型及方法

    数据类型及方法 数据类型 number 不区分整数和浮点数 string 字符串 boolean true / false 布尔 object null 数组 function 函数 undefine ...

  7. 负载均衡器nginx和ribbon区别

    1,nginx 是服务器端的负载均衡器,所有请求发送到nginx之后,nginx通过反向代理的功能分发到不同的服务器,做负载均衡 2,ribbon是客户端的负载均衡器,他是通过将eureka注册中心上 ...

  8. GCDAsyncSocket 笔记

    https://www.dzliving.com/2019/03/26/gcdasyncsocket-%E7%9A%84%E5%B0%81%E8%A3%85%E4%B8%8E%E4%BD%BF%E7% ...

  9. 使用Git pull文件时,出现"error: RPC failed; curl 18 transfer closed with outstanding read data remaining"

    error: RPC failed; curl transfer closed with outstanding read data remaining fatal: The remote end h ...

  10. Java数据类型与mysql对应表