Codeforces Round #600 (Div. 2) D题【并查集+思维】

题意:给你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题【并查集+思维】的更多相关文章
- 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 ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
- 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 ...
- Codeforces Round #260 (Div. 1) C. Civilization 并查集,直径
C. Civilization Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/probl ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...
- Codeforces Round #623 (Div. 2) D.Recommendations 并查集
ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类 ...
- Codeforces Round #485 (Div. 2) C题求三元组(思维)
C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)
#include<bits/stdc++.h>using namespace std;vector<int>g[2007];int fa[2007],vis[2007],num ...
随机推荐
- Burp Suite的安装与使用
Burp Suite是一个集成化的渗透测试工具,它集合了多种渗透测试组件,使我们自动化地或手工地能更好的完成对web应用的渗透测试和攻击.在渗透测试中,我们使用Burp Suite将使得测试工作变 ...
- 利用Python进行数据分析 第7章 数据清洗和准备(2)
7.3 字符串操作 pandas加强了Python的字符串和文本处理功能,使得能够对整组数据应用字符串表达式和正则表达式,且能够处理烦人的缺失数据. 7.3.1 字符串对象方法 对于许多字符串处理和脚 ...
- Python Http-server 使用
Python内置的下载服务器 http.server Python的Web服务器 python2 中SimpleHTTPServer python3 中 http.server 执行 python ...
- WUSTOJ 1311: 开心的金明(Java)动态规划-01背包
题目链接:
- Python3遍历指定文件夹下所有文件及文件夹
采用os模块儿: import os def get_filelist(dir): for home, dirs, files in os.walk(dir): print("####### ...
- sqlserver exists 与 in 的区别
使用 EXISTS 方式 select * from A a where EXISTS(select b.mainInfoId from B b where b.mainInfoId=a.main ...
- C++ 去掉字符串的首尾空格和全部空格
#include <iostream>#include <string>using namespace std; //去掉收尾空格string& ClearHeadTa ...
- sql 语句实现一串数字位数不足在左侧补0的技巧
https://www.cnblogs.com/mylydg/p/5725189.html 在日常使用sql做查询插入操作时,我们通常会用到用sql查询一串编号,这串编号由数字组成.为了统一美观,我们 ...
- Windows Mobile设备中心不能正常运行
1.开始-->运行,输入services.msc回车 2.在打开的服务界面中,找到“基于Windows Mobile 2003的连接设备” 3.打开的属性 ,找到登录项,登录身份选择“本地系统账 ...
- RZ70注册SLD
本文的将S4 abap系统向PO JAVA系统中注册. S4 QASERPAP01 NR=60 ASCS=61 PO QASPISAP01 NR=60 SCS=61 http://qaspisap01 ...