D. Harmonious Graph

好后悔在写这个题之前浪费了几分钟时间,不然我就写出来了....

因为他就是连通块之间的合并问题,所以就用并查集就好了

复杂度好像也只是线性的吧...

然后就A了

代码:

// Created by CAD on 2019/12/4.
#include <bits/stdc++.h>
using namespace std; const int maxn=2e5+5;
int f[maxn];
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
int last[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m; cin>>n>>m;
for(int i=1;i<=n;++i)
f[i]=i;
for(int i=1,u,v;i<=m;++i)
{
cin>>u>>v;
int f1=find(u),f2=find(v);
if(f1!=f2)
f[f1]=f2;
}
int ans=0;
for(int i=1;i<=n;++i)
{
int t=find(i);
if(last[t])
{
for(int j=last[t]+1;j<i;j++)
{
int temp=find(j);
if(temp!=t)
f[temp]=t,ans++;
}
}
last[t]=i;
}
cout<<ans<<endl;
return 0;
}

Harmonious Graph的更多相关文章

  1. 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[ ...

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

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

  3. D - Harmonious Graph

    题目大意: n个点,m条边,两个数l和r,如果l和r相连接,那么对于l和r之间值任意一个数都要和l相连.问达到这一目的需要添加的边的最小数量. 题解: 我们首先要找到当前连通块中最大的那个点,也就是说 ...

  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. redis cluster异地数据迁移,扩容,缩容

    由于项目的服务器分布在重庆,上海,台北,休斯顿,所以需要做异地容灾需求.当前的mysql,redis cluster,elastic search都在重庆的如果重庆停电了,整个应用都不能用了. 现在考 ...

  2. svnkit 用java 操作 svn

    官网 https://svnkit.com/ https://blog.csdn.net/Hui_hai/article/details/80318518 https://blog.csdn.net/ ...

  3. iOS-OC中常见的一些宏

    /* 1. 颜色 */ #define PCBRGBColorA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b ...

  4. ASE19团队项目 beta阶段 model组 scrum4 记录

    本次会议于12月5日,19时30分在微软北京西二号楼sky garden召开,持续10分钟. 与会人员:Lei Chai, Linfeng Qi, Xueqing Wu, Yutong Ling (Z ...

  5. el-table——可合并单元格的表格

    <el-table v-loading="loading" :data="tableData" border :span-method="col ...

  6. flex布局实战

    1.实现盒子的水平垂直居中 .parent{ width:200px; height:200px; display:flex; align-items: center; justify-content ...

  7. Oracle 数据 查询 一对多 取最新一条非 0 数据

    主键id     待查字段     日期 1            6             2019/5/1 1            0             2019/5/2 需求: 找出 ...

  8. git日常命令

    克隆项目:git clone addr dir 切换分支,分支并没有 git checkout -b production origin/production 分支已存在,单纯切换分支 git che ...

  9. 【转】Linux编译链接问题----静态库和动态库

    Linux静态库和动态库的命名规则 静态函数库 静态库的名字一般是libxxx.a,利用静态库编译生成的文件比较大,因为整个静态库所有的数据都会被整合进目标代码中. a) 优点: 编译后,可执行文件不 ...

  10. Paper Reading:Faster RCNN

    Faster R-CNN 论文:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 发表时间: ...