题意:给你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. Sublime实现自动排版

    sublime功能很强大, 但是使用sublime就可以实现代码自动重新缩进,使代码缩进重排 方法:Ctrl+A选中全部内容,然后在菜单中选择Edit->Line->Reindent

  2. WUSTOJ 1321: Alphabet Cookies(Java)字符统计

    题目链接:1321: Alphabet Cookies Description Kitty likes cookies very much, and especially the alphabet c ...

  3. C#简单工厂案例

    using System; namespace Application { class JianDanGongChang { static void Main(string[] args) { Fac ...

  4. Dev GridControl 子集合标题

    显示效果: 设置: this.gridView3.OptionsView.ShowViewCaption = true; this.gridView3.ViewCaption = "资产明细 ...

  5. dev 从表处理

    从表列名,从表选中行和主表选中一样,var selectrow = detailView.GetRow(detailView.FocusedRowHandle) as obj; private voi ...

  6. solr的post.jar

    http://iamyida.iteye.com/blog/2207920   跟益达学Solr5之玩转post.jar

  7. nginx的access_log与error_log

     参考文章:https://juejin.im/post/5aa09bb3f265da238f121b6c 本篇文章主要介绍一下 nginx 服务器两种日志查看:access_log.error_lo ...

  8. Java 之 字符流

    一.字符流 当使用字节读取文本文件时,可能会有一个小问题,就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储.所以 Java 提供了一些字符类,以字符为单位读写数据, ...

  9. ABAP-信息结构S901/S902程序问题

    1.问题 信息结构请求传入正式环境后,RMCX0070/RMCX0071程序执行DUMP,无法调用程序SAPFMCBF中的子FORM程序 MCB1_901 或是 MCB2_901 . 2.经查SAPF ...

  10. SR开启时LOG_MODE必须是normal

    SR开启时LOG_MODE必须是normal 需要一个初始化备份,