CF

luogu

先讲两个靠谱的做法

1.首先因为有n个点,m条不存在的边,所以至少存在一个点,和m/n个点之间没边,所以把这个点找出来,连一下其他相连的点,这样还剩m/n个点没确定在哪个联通块,而这些点最多和n-1个点有边,所以从这些点暴力合并即可

2.开一个队列/set/链表维护没有确定在哪个联通块的边,每次先取出一个点,然后往点集里尽量连边,再把连出的点放到队列里继续连边合并,这样每次都能搜出一个联通块

下面是假算法

每次找一个没标记的点,打标记,爆枚所有存在的边,和对应点连上,在把刚刚扫到的点打标记

然额是错的,详见我的提交记录

但是懒得改了

#include<bits/stdc++.h>
#define LL long long
#define db double
#define il inline
#define re register using namespace std;
const int N=2e5+10;
il LL rd()
{
LL x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
int to[N<<1],nt[N<<1],hd[N],dg[N],tot=1;
il void add(int x,int y)
{
++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot,--dg[x];
++tot,to[tot]=x,nt[tot]=hd[y],hd[y]=tot,--dg[y];
}
bool v[N],vv[N];
int n,m,ff[N],sz[N],s[N],a[N],ta;
il int findf(int x){return ff[x]==x?x:ff[x]=findf(ff[x]);}
il bool cmp(int a,int b){return dg[a]>dg[b];} int main()
{
n=rd(),m=rd();
for(int i=1;i<=n;++i) ff[i]=i,sz[i]=1,dg[i]=n-1,s[i]=i;
for(int i=1;i<=m;++i) add(rd(),rd());
sort(s+1,s+n+1,cmp);
for(int i=1;i<=n;++i)
if(!v[s[i]]||n<=5000) //把数据小的部分暴力处理就能过了qwq
{
v[s[i]]=vv[s[i]]=1;
for(int j=hd[s[i]];j;j=nt[j]) vv[to[j]]=1;
for(int j=1;j<=n;++j)
if(!vv[j])
{
v[j]=1;
int x=findf(s[i]),y=findf(j);
if(x^y) ff[y]=x,sz[x]+=sz[y];
}
vv[s[i]]=0;
for(int j=hd[s[i]];j;j=nt[j]) vv[to[j]]=0;
}
for(int i=1;i<=n;++i)
if(findf(i)==i) a[++ta]=sz[i];
sort(a+1,a+ta+1);
printf("%d\n",ta);
for(int i=1;i<=ta;++i) printf("%d ",a[i]);
return 0;
}

CF920E Connected Components?的更多相关文章

  1. [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), ...

  2. PTA Strongly Connected Components

    Write a program to find the strongly connected components in a digraph. Format of functions: void St ...

  3. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  4. [Redux] Using withRouter() to Inject the Params into Connected Components

    We will learn how to use withRouter() to inject params provided by React Router into connected compo ...

  5. [Locked] Number of Connected Components in an Undirected Graph

    Number of Connected Components in an Undirected Graph Given n nodes labeled from 0 to n - 1 and a li ...

  6. [Swift]LeetCode323. 无向图中的连通区域的个数 $ 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), ...

  7. Codeforces E - Connected Components?

    E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...

  8. 6-19 Count Connected Components(20 分)

    Write a function to count the number of connected components in a given graph. Format of functions: ...

  9. 323. 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 n ...

随机推荐

  1. QML学习笔记(三)-引入Font-awesome

    作者: 狐狸家的鱼 Github: 八至 1.首先得在qml文件夹下建立字体文件,将font-awesome放入进去 2.然后在main.cpp中注册字体 引入中一定要写上 引用字体 引用字体得路径一 ...

  2. 洛谷P4316 绿豆蛙的归宿

    一眼看去,这不是高斯消元吗? 然后发现数据范围是100000... 然后发现是DAG...直接拓扑序递推即可. 边(x, y,z)的贡献是P(x) * z / out[x] #include < ...

  3. 第二十三篇-ubuntu18.04怎么下载播放器以及如何设置默认播放器

    下载播放器 安装命令:sudo apt-get install smplayer再安装解码器: 安装win32codecs包 如果是intel的CPU 代码: sudo apt-get install ...

  4. 文献笔记:Genome-wide associations for birth weight and correlations with adult disease

    该文献纳入了EGG(Early Growth Genetics Consortium)和UK biobank两大数据库,分为欧洲祖先和非欧洲祖先群体.这两个数据用到的样本量分别如下: Early Gr ...

  5. RabbitMQ入门-发布订阅模式

    兔子的Publish/Subscribe是这样的: 有个生产者P,X代表交换机,交换机绑定队列,消费者从队列中取得消息.每次有消息,先发到交换机中,然后由交换机负责发送到它已知的队列中. 生产者代码: ...

  6. c#线程1

    开启一个线程的方式: 方式一:Thread t1 = new Thread(Method_1); t1.Start();方式二:委托 Action ac = Method_1; ac.BeginInv ...

  7. 关于Navicat远程连接远程服务器的mysql 报错问题

    我们连接远程服务器的mysql,如果出现问题,很大问题会出在服务器的端口和授权问题 首先我们通过 1:netstat -an|grep 3306 来查看mysql默认的端口3306是否开启,允许哪个i ...

  8. 2017-12-15python全栈9期第二天第七节之数字转换成布尔值

    #!/user/bin/python# -*- coding:utf-8 -*-print(bool(2))

  9. List数组

    大家好,我是蜀云泉.我的博文之中存在的不足之处希望大家包涵. 今天学习unity时,在实现某个功能的脚本中发现了List数组.关于List数组的问题我在学C#时已经接触了一点,但是我比较粗心和浮躁以前 ...

  10. python 管道 事件(Event) 信号量 进程池(map/同步/异步)回调函数

    ####################总结######################## 管道:是进程间通信的第二种方式,但是不推荐使用,因为管道会导致数据不安全的情况出现 事件:当我运行主进程的 ...