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的更多相关文章
- 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[ ...
- Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)
题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...
- D - Harmonious Graph
题目大意: n个点,m条边,两个数l和r,如果l和r相连接,那么对于l和r之间值任意一个数都要和l相连.问达到这一目的需要添加的边的最小数量. 题解: 我们首先要找到当前连通块中最大的那个点,也就是说 ...
- Codeforces Round #600 (Div. 2)
传送门 A. Single Push 直接乱搞即可. Code /* * Author: heyuhhh * Created Time: 2019/11/16 22:36:20 */ #include ...
- CF Round #600 (Div 2) 解题报告(A~E)
CF Round #600 (Div 2) 解题报告(A~E) A:Single Push 采用差分的思想,让\(b-a=c\),然后观察\(c\)序列是不是一个满足要求的序列 #include< ...
- [开发笔记] Graph Databases on developing
TimeWall is a graph databases github It be used to apply mathematic model and social network with gr ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- POJ 2125 Destroying the Graph 二分图最小点权覆盖
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8198 Accepted: 2 ...
- [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), ...
随机推荐
- (七)Activiti之历史活动查询和历史任务查询和流程状态查询
一.历史活动查询 本章案例是基于上一章节案例的基础上,流程走完后进行测试的,也就是下图的流程从学生请假到班主任审批都已经完成,本章用来测试查询历史活动和历史任务的 activiti5的历史活动包括所有 ...
- Bootstrap3 CDN 使用手册
一.一般功能 <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel=" ...
- Https请求被中止: 未能创建 SSL/TLS 安全通道
可以参考https://www.cnblogs.com/ccsharp/p/3270344.html 和https://blog.csdn.net/baidu_27474941/article/det ...
- stm32 待机模式
低功耗模式 降低系统时钟速度 不使用APBx和AHB外设时,将对应的外设时钟关闭 睡眠模式(Cortex™-M3内核停止,所有外设包括Cortex-M3核心的外设,如NVIC.系统时钟(SysTick ...
- tornado基本使用一
一.tornado web程序编写思路 . 创建web应用实例对象,第一个初始化参数为路由映射列表 . 定义实现路由映射列表中的handler类 . 创建服务器实例, 绑定服务器端口 . 启动当前线程 ...
- Signal Processing and Pattern Recognition in Vision_15_RANSAC:Random Sample Consensus——1981
此部分是 计算机视觉中的信号处理与模式识别 与其说是讲述,不如说是一些经典文章的罗列以及自己的简单点评.与前一个版本不同的是,这次把所有的文章按类别归了类,并且增加了很多文献.分类的时候并没有按照传统 ...
- docker alpine wkhtmltopdf
截止2019.08 wkhtmltopdf 还没有 alpine 的版本 如需使用 需要在 alpine 环境中编译 生成 wkhtmltopdf (使用 apk add wkhtmltopdf ...
- jade-mixin 代码的重用
有时候页面有好多个区块,比如列表区块,但是他们代码结构又是一模一样的怎么弄?jade天生就是节约成本,节约时间的,mixin就是让代码块可以重用的函数 mixin lession p jade s ...
- Java基础 static限定符的使用 以及【 static实现的 singleton(单例)设计模式】
static实现的 singleton(单例)设计模式 /** static实现的 singleton设计模式 , 使得一个类只能够创建一个static对象 */ 模板设计结构: package Co ...
- .NET 推荐博客
燎原之星的博客 农码一生博文索引 http://www.cnblogs.com/zhaopei/p/Indexes.html 那些年搞不懂的术语.概念:协变.逆变.不变体 http://www.cnb ...