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. Docker自动补全容器名

    Zsh Place the completion script in your /path/to/zsh/completion (typically ~/.zsh/completion/): 下载自动 ...

  2. A1120. Friend Numbers

    Two integers are called "friend numbers" if they share the same sum of their digits, and t ...

  3. 2. github创建 git仓库,克隆,拉取和推送操作(所有的git命令前提是一定在当前项目目录下)

    步骤: 1.点击小猫,回到初始页面 2.点击start a project,首先会出现一个验证email地址(我们注册的时候,有一个email输入,进去邮箱验证一下) 3.重新点击start a pr ...

  4. C++ template一些体悟(3)

    其实没啥体悟,因为还没有感受到这些例子的作用,记一下先 #include <iostream> using namespace std; class alloc { }; template ...

  5. 关于python类变量和实例变量

    今天在看python的类和实例的时候,突然发现了一个以前遗漏的点,也就是类变量和实例变量.首先需要理解一下类变量和实例变量的概念. 类全局变量:在类中定义,对类和由类生成的实例生效,如果通过方法对类变 ...

  6. AT2165 Median Pyramid Hard 二分答案 脑洞题

    无论再来多少次也不可能想到的写法. 二分一个最小的顶端值\(k\),大于设为\(1\)小于等于设为\(0\),可以证猜出来(你跟我说这可以?)如果存在两个连在一起的0/1那么它们会一直往上跑,还可以很 ...

  7. (LIS DP) codeVs 1044 拦截导弹

    题目描述 Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某 ...

  8. mysql5.7 on windows

    1.下载zip包:https://dev.mysql.com/downloads/file/?id=476696 2.解压到E盘3.执行命令 初始化 E:/mysql-5.7.22-winx64/bi ...

  9. chrome截图全网页

    1.F12 2.ctrl+shift+p 3.输入:capture 4.选择Capture full size screenshot

  10. 神经网络4_BP神经网络

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...