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. C# Socket的安全关闭

    网络编程中,socket的安全关闭方法 /// <summary> /// Close the socket safely. /// </summary> /// <pa ...

  2. 解决plink报错:.bim file has a split chromosome. Use --make-bed by itself to remedy this.

    由于plink1.9和1.07这两个版本互掐,经常出现各种不兼容问题,“.bim file has a split chromosome.  Use --make-bed by itself to r ...

  3. 第十二节、尺度不变特征(SIFT)

    上一节中,我们介绍了Harris角点检测.角点在图像旋转的情况下也可以检测到,但是如果减小(或者增加)图像的大小,可能会丢失图像的某些部分,甚至导致检测到的角点发生改变.这样的损失现象需要一种与图像比 ...

  4. zookeeper的监控

    1.idea插件: zookeeper https://blog.csdn.net/long290046464/article/details/52974061 优点:直接查看,不用来回切换监控 缺点 ...

  5. socket编程以及select、epoll、poll示例详解

    socket编程socket这个词可以表示很多概念,在TCP/IP协议中“IP地址 + TCP或UDP端口号”唯一标识网络通讯中的一个进程,“IP + 端口号”就称为socket.在TCP协议中,建立 ...

  6. HTML5(FileRdeader)

    主要作用: 异步读取存储在用户计算机上的文件(或原始缓冲区的内容),使用File或Blob对象指定要读取的文件或数据.其中File是input元素上选择文件后返回的FileList对象,也可以是拖放操 ...

  7. redis服务意外停止

    本地windows的redis一直好的,今天突然项目报错无法连接,不得解 看了下日志, [4824] 04 Mar 11:15:45.714 # Accepting client connection ...

  8. Tornado基于MiddleWare做中间件

    详细代码如下: 在app.py里添加 # -*- coding:utf-8 -*- from tornado.ioloop import IOLoop from tornado.web import ...

  9. kaggle竞赛入门整理

    1.Bike Sharing Demand kaggle: https://www.kaggle.com/c/bike-sharing-demand 目的:根据日期.时间.天气.温度等特征,预测自行车 ...

  10. sping入门

    SpringMVC 1.RequestMapping 处理地址映射请求 参数: value, produces, method, params, headers value:指定请求的实际地址 1.最 ...