CF920E Connected Components?
先讲两个靠谱的做法
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?的更多相关文章
- [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), ...
- PTA Strongly Connected Components
Write a program to find the strongly connected components in a digraph. Format of functions: void St ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- [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 ...
- [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 ...
- [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), ...
- Codeforces E - Connected Components?
E - Connected Components? 思路: 补图bfs,将未访问的点存进set里 代码: #include<bits/stdc++.h> using namespace s ...
- 6-19 Count Connected Components(20 分)
Write a function to count the number of connected components in a given graph. Format of functions: ...
- 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 ...
随机推荐
- Dubbo x Cloud Native 服务架构长文总结(很全)
Dubbo x Cloud Native 服务架构长文总结(很全) mercyblitz SpringForAll社区 3天前 分享简介 Cloud Native 应用架构随着云技术的发展受到业界特别 ...
- bzoj2553 禁忌
题目链接 题意 给出一个\(n\)个字符串的字典.对于一个字符串,他的贡献是这个字符串中最多的在字典中出现的不重叠子串的数量. 然后问一个长度为\(len\)的,字符集为前\(alphabet\)个字 ...
- [luogu1503][鬼子进村]
题目链接 思路 将哪些村庄已经被摧毁了放到treap里.查询的时候如果当前村庄已经被毁了,那么就可以直接输出0.不然就输出这个村庄的后继-前驱-1.原因显然 代码 #include<cstdio ...
- MD5加密解密类(asp.net)&使用MD5过时处理
加密类 #region ========加密======== /// <summary> /// 加密 /// </summary> /// <param name=&q ...
- win10 python3.5 自动补全设置
https://www.cnblogs.com/lgh344902118/p/8521437.html # python startup file import readline import rlc ...
- 全局拦截各种http请求
http请求无非就是ajax.src.href.表单 function hookAJAX() { XMLHttpRequest.prototype.nativeOpen = XMLHttpReques ...
- Dream team: Stacking for combining classifiers梦之队:组合分类器
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- Linux下查看线程数的几种方法汇总
Linux下查看线程数的几种方法汇总 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux下查看某个进程的线程数量 pstree命令以树状图显示进程间的关系(display ...
- centos的用户、组权限、添加删除用户等操作的详细操作命令
1.Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还 有组账户所谓组账户就是用户账户的集合,centos组中有两种类型, ...
- Java Web之HTML5
终于学到Java Web这一章节了,首先来了解一下HTML5的一些新知识点吧,我直接贴出HTML5代码看一下: <!DOCTYPE html> <html lang="en ...