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), ...
随机推荐
- WebApi 全局异常与局部异常
全局异常过滤器 public class ApiExceptionFilter:ExceptionFilterAttribute { private IHostingEnvironment _env; ...
- .net core mvc + mysql dbfirst 生成 ado.net 数据模型
1.点击“工具”->“NuGet包管理器”->“程序包管理器控制台” 安装一下包 Install-Package MySql.Data.EntityFrameworkCore -Pre I ...
- SIP:100rel 扩展
SIP:100rel 扩展 100rel扩展即是对中间状态响应的确认(即1xx的响应码).原先在sip里,只有针对invite请求的200ok响应才会有ack,那么当中间状态响应携带重要的会话参数信息 ...
- [书籍翻译] 《JavaScript并发编程》 第二章 JavaScript运行模型
本文是我翻译<JavaScript Concurrency>书籍的第二章 JavaScript运行模型,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- 使用Canvas压缩图片
讲干货,不啰嗦,当涉及对图片有质量压缩要求的时候,可以使用Canvas实现图片压缩. 步骤: 1.获取img元素,既要压缩的图片 2.创建canvas对象 3.使用canvas的drawImage方法 ...
- el-table——可合并单元格的表格
<el-table v-loading="loading" :data="tableData" border :span-method="col ...
- 转:基于Maven管理的JavaWeb项目目录结构参考
通常在创建JavaWeb项目时多多少少都会遵循一些既定的比较通用的目录结构,下面分享一张基于Maven管理的JavaWeb项目目录结构参考图: 上图仅是参考,不同项目不同团队都有自己的约定和规范. 个 ...
- 12.自定义v-过渡动画前缀
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 测试某网站的SMS验证码
to=18911121211&sms_type=sms_registration&captcha_num=9JCMw4yN5EjI6ISYoNGdwF2YiwiIw5WNwlmb3xm ...
- Java 中的多态,一次讲个够之接口实现关系中的多态
上文还没有写完,这一篇继续 Java 中的多态,一次讲个够之继承关系中的多态 https://www.cnblogs.com/qianjinyan/p/10824576.html 接口实现关系,和继承 ...