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. Surface Pro 4 系统优化全教程及QA

    甲.如何根据你的需求选择相应版本的surface1.Core M3-6Y30,Core i5-6300U,Core i7-6650U的性能差距有多大?2.HD 515,HD520,Iris 540的性 ...

  2. LOJ#2720 你的名字

    题意:给定母串s和若干个询问.每个询问是一个串t和两个数l,r,表示求t中有多少个本质不同的子串没有在s[l,r]中出现过. 解:我写的并不是正解......是个毒瘤做法.只在loj上面卡时过了就写l ...

  3. Django 分页器的使用

    Django 分页器的使用 Django作为Python Web开发框架的一哥,提供了企业级网站开发所需要的几乎所有功能,其中就包括自带分页功能.利用Django自带的Paginator类,我们可以很 ...

  4. (转)MySQL中In与Exists的区别

    背景:总结mysql相关的知识点. 如果A表有n条记录,那么exists查询就是将这n条记录逐条取出,然后判断n遍exists条件. select * from user where exists s ...

  5. How MVC pattern Flows

    以上MVC流程中Model和View不存在依赖关系 以上MVC流程View和Model存在耦合关系(依赖关系越少越好)

  6. MySQL数据库简单查询

    --黑马程序员 DQL数据查询语言 数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端.查询返回的结果集是一张虚拟表. 查询关键字:SELECT 语法: SELECT 列名 FRO ...

  7. numpy的使用方法

    一.numpy快速入门 1.什么是numpy: numpy是python的一个矩阵类型,提供了大量矩阵处理的函数,非正式来说,就是一个使运算更容易,执行更迅速的库,因为它的内部运算是通过c语言而不是p ...

  8. 【译】12. Java反射——类的动态加载和重新加载

    原文地址:http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html 博主最近比较忙,争取每周翻译 ...

  9. (set)产生冠军 hdu2094

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. 爬虫之requests请求库高级应用

    1.SSL Cert Verification #证书验证(大部分网站都是https) import requests respone=requests.get('https://www.12306. ...