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), ...
随机推荐
- (三)easyUI之树形组件
一.同步树 1.1 概念 所有节点一次性加载完成 1.2 案例 1.2.1 数据库设计 1.2.2 编码 index.jsp <%@ page language="java" ...
- Linux下源码包安装Swoole及基本使用 转
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/msllws/article/details ...
- 【leetcode】575. Distribute Candies
原题 Given an integer array with even length, where different numbers in this array represent differen ...
- MySQL全同步复制基于GR集群架构实现(Centos7)
目录 一. 理论概述 概述 二. 部署 向组加入新节点 测试 三.总结 一. 理论概述 概述 本案例操作的是针对于MySQL的复制类型中的全同步复制,对几种复制类型简单总结下: 异步复制:MySQL默 ...
- Image Processing and Analysis_15_Image Registration:Multi-modal volume registration by maximization of mutual information——1996
此主要讨论图像处理与分析.虽然计算机视觉部分的有些内容比如特 征提取等也可以归结到图像分析中来,但鉴于它们与计算机视觉的紧密联系,以 及它们的出处,没有把它们纳入到图像处理与分析中来.同样,这里面也有 ...
- http服务详解(2)——httpd的配置文件常见设置
HTTP服务器应用 http服务器程序 httpd apache nginx lighttpd 应用程序服务器 IIS .asp tomcat .jsp jetty 开源的servlet容器,基于Ja ...
- 01 Windows编程——Hello World
源码 #include "stdafx.h" #include<Windows.h> int WINAPI WinMain(HINSTANCE hInst,HINSTA ...
- 页面Header自适应屏幕
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- Excel 教程一
俗话说,工欲善其事,必先利其器,那么我们今天就先来看一下这个excel软件的一些主要功能菜单. 一.功能区菜单 功能区菜单主要包括: 文件菜单: 主要用于新建文件,保存文件,另存为文件,打开文件,打 ...
- mongodb的基本操作之数据删除
删除操作使用remove进行,数据的删除与查询类似,接受一个参数,与查询不同的是,为了防止误操作,删除操作不允许不传参数 比如 db.test_collection.remove() 会报错 Erro ...