题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边。协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边。

思路:通过并查集不断维护连通量的最大编号的节点,然后遍历即可。

代码:

 #include<bits/stdc++.h>

  using namespace std;
  #define int long long
  #define N 1005000
  int f[N];int n,m;
  int getf(int v){// 并查集模板
      if(v==f[v])
          return  f[v];
      else{
          f[v]=getf(f[v]);
          return f[v];
      }
  }
  void init(){
      ;i<=n;i++)
          f[i]=i;
  }
  void merge(int u,int v){
      int t1=getf(u);
      int t2=getf(v);
      if(t1==t2){
          return ;
     }
     if(t1>t2){
         f[t2]=t1;
     }else{
         f[t1]=t2;
     }
  }
  int vis[N];
  signed main(){
     cin>>n>>m;
     int x,y;
     init();
     ;i<=m;i++){
         cin>>x>>y;
         merge(x,y);
     }
     ;
     ;i<=n;i++){
         if(vis[i])
             continue;
         int k=getf(i);
         for(int j=i;j<=(k=getf(i));j++){
             vis[j]=;
             if(getf(i)==getf(j))
                 continue;
             ans++;
             merge(i,j);
         }
     }
     cout<<ans<<'\n';
      ;
  }

Codeforces Round #600 (Div. 2) D题【并查集+思维】的更多相关文章

  1. Codeforces Round #346 (Div. 2) E题 并查集找环

    E. New Reform Berland has n cities connected by m bidirectional roads. No road connects a city to it ...

  2. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  3. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  4. Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径

    C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...

  5. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  6. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

  7. Codeforces Round #623 (Div. 2) D.Recommendations 并查集

    ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...

  8. Codeforces Round #485 (Div. 2) C题求三元组(思维)

    C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)

    #include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...

随机推荐

  1. PAT甲级 Dijkstra 相关题_C++题解

    Dijkstra PAT (Advanced Level) Practice Dijkstra 相关题 目录 <算法笔记>重点摘要 1003 Emergency (25) <算法笔记 ...

  2. FZU2018级算法第五次作业 missile(排序+枚举)

    在解题报告之前,首先对同一次作业中另外一题(求逆序对)某人在未经冰少允许情况下,擅自登录冰少账号,原模原样剽窃冰少代码,并且最终还被评为优秀作业的行为表示严正抗议! 题目大意: 二维平面上给出 n 个 ...

  3. laravel框架视图中常用的逻辑结构forlese,foreach,ifelse等

    if 和else @if($name === 1) 这个数字是1 @else 这个数字非1 @endif switch @switch($name) @case(1) 变量name == 1 @bre ...

  4. notepad++安装markdown

    notepad++ 安装markdown安装markdown插件一.下载最新的markdown插件, github:https://github.com/nea/MarkdownViewerPlusP ...

  5. visual studio code编辑python文件

    visual studio code 安装.通过360软件管家,查找visual studio code 下载安装即可 设置visual studio code为中文 打开进入软件,Ctrl + Sh ...

  6. hadoop mapper reducer

    Local模式运行MR流程------------------------- 1.创建外部Job(mapreduce.Job),设置配置信息 2.通过jobsubmitter将job.xml + sp ...

  7. S04_CH02_工程移植ubuntu并一键制作启动盘

    S04_CH02_工程移植ubuntu并一键制作启动盘 2.1概述 2.2搭建硬件系统 本章硬件工程还是使用<S04_CH01_搭建工程移植LINUX/测试EMMC/VGA>所搭建的VIV ...

  8. TCP--粘包拆包,netty的解决方式

    TCP基于链接的协议,并且保证有序性. 但是,每个包的长度,需要明确,否则会发生粘包现象. 以下示例为一个自定义协议的例子,其中包含了拆包的内容. 所有的类: 协议类: public class Pe ...

  9. 怎样重启ssh服务

    尝试下面两个命令: service sshd restart systemctl restart sshd.service

  10. [转发]C++中new和malloc的区别

    原文地址:https://blog.csdn.net/linux_ever/article/details/50533149 new与malloc的10点区别 1. 申请的内存所在位置 new操作符从 ...