【题意分析】

  给你一张无向图,求其补图的联通块数及各个联通块大小。

【解题思路】

  暴搜!

  然而n2会T怎么办?

  仔细观察发现m远小于n2,也就是说这是一张极其稠密的补图。

  这时就要用到黑科技了:floodfill

  用邻接表维护原图的边,用链表维护当前剩余可选点,每次从队首出发从链表里找补图的边,把这些边对应的点入队并从链表里删去。

  这样,我们构造一种最坏的情况来卡这个算法:

  假设前m/n个点每个点都只和一个点不相连,这样对于每个点都要遍历链表中的所有点,此时复杂度是O((m/n)*n)=O(m)。

  因为前面已经把m条边都分配完了,接下来的第一个点就O(n)把链表清空了。之后的点全都是O(1)发现链表已被清空。

  这样总复杂度就是O(m+n)了。

【参考代码】

 #include <bits/stdc++.h>
#define range(i,c,o) for(register int i=(c);i<(o);++i)
#define dange(i,c,o) for(register int i=(c);i>(o);--i)
#define forin(i,t,p) for(t:: iterator i=p. begin();i!=p. end();++i)
#define dorin(i,t,p) for(t::reverse_iterator i=p.rbegin();i!=p.rend();++i)
using namespace std; #define __debug
#ifdef __debug
#define Function(type) type
#define Procedure void
#else
#define Function(type) __attribute__((optimize("-O2"))) inline type
#define Procedure __attribute__((optimize("-O2"))) inline void
#endif #ifdef __int128_t
typedef __int128_t integer;
#else
typedef long long integer;
#endif //quick_io {
Function(integer) getint()
{
char c=getchar(); for(;!isdigit(c)&&c!='-';c=getchar());
short s=; for(;c=='-';c=getchar()) s*=-; integer r=;
for(;isdigit(c);c=getchar()) (r*=)+=c-''; return s*r;
}
//} quick_io static int n=getint(); //list {
int suc[],pre[];
Procedure clear()
{
range(i,,n+) suc[i]=i+,pre[i]=i-;
suc[]=,pre[n+]=n;
}
Procedure erase(const int&x)
{
pre[suc[x]]=pre[x],suc[pre[x]]=suc[x];
}
//} list bool tag[]={},vis[]={};
vector<int> edg[]; queue<int> que;
Function(int) floodfill(const int&rt)
{
int ret=; erase(rt);
for(que.push(rt);!que.empty();que.pop())
{
int fr=que.front(); vis[fr]=,++ret;
forin(i,vector<int>,edg[fr]) tag[*i]=;
for(int i=suc[];i<=n;i=suc[i])
{
if(!tag[i]) erase(i),que.push(i);
}
forin(i,vector<int>,edg[fr]) tag[*i]=;
}
return ret;
} int rec[];
int main()
{
for(int m=getint();m--;)
{
int u=getint(),v=getint();
edg[u].push_back(v),edg[v].push_back(u);
}
clear(); int cnt=;
range(i,,n+) if(!vis[i]) rec[cnt++]=floodfill(i);
sort(rec,rec+cnt),printf("%d\n",cnt);
range(i,,cnt) printf("%d ",rec[i]);
return putchar('\n'),;
}

bzoj1098题解的更多相关文章

  1. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  2. noip2016十连测题解

    以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...

  3. BZOJ-2561-最小生成树 题解(最小割)

    2561: 最小生成树(题解) Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1628  Solved: 786 传送门:http://www.lyd ...

  4. Codeforces Round #353 (Div. 2) ABCDE 题解 python

    Problems     # Name     A Infinite Sequence standard input/output 1 s, 256 MB    x3509 B Restoring P ...

  5. 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解

    题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...

  6. 2016ACM青岛区域赛题解

    A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  7. poj1399 hoj1037 Direct Visibility 题解 (宽搜)

    http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...

  8. 网络流n题 题解

    学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...

  9. CF100965C题解..

    求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...

随机推荐

  1. java中循环删除list中元素的方法

    重点哈 印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接 ...

  2. 【Flutter学习】页面布局之其它布局处理

    一,概述 Flutter中拥有30多种预定义的布局widget,常用的有Container.Padding.Center.Flex.Row.Colum.ListView.GridView.按照< ...

  3. Wireshark协议分析1

    一.界面简介   1.抓包工具栏 2.文件工具栏 3.包定位工具栏 4.颜色以及滚动界面工具栏 5.数据包列表字体定义工具栏 6.首选项工具栏 二.过滤规则 1.过滤 IP ip.src eq 192 ...

  4. Linux系统之-TCP-IP链路层

    一.基本 网络层协议的数据单元是 IP 数据报 ,而数据链路层的工作就是把网络层交下来的 IP 数据报 封装为 帧(frame)发送到链路上,以及把接收到的帧中的数据取出并上交给网络层. 为达到这一目 ...

  5. rabbitmq集群-2

    rabbitmq集群 原文地址:https://www.cnblogs.com/lion.net/p/5725474.html rabbitmq集群介绍 rabbitmq有3种模式,但集群模式是2种. ...

  6. 修改PhpStorm创建Php类文件时头部作者

    原文链接:https://segmentfault.com/a/1190000015617093 首先打开phpstorm后找到Setting/Editor/Inspections/PHP/File ...

  7. HTML5: HTML5 Web 存储

    ylbtech-HTML5: HTML5 Web 存储 1.返回顶部 1. HTML5 Web 存储 HTML5 web 存储,一个比cookie更好的本地存储方式. 什么是 HTML5 Web 存储 ...

  8. nginx logformat说明

    记录一下nginx logformat的相关说明 log_format格式变量:$remote_addr  #记录访问网站的客户端地址$remote_user  #远程客户端用户名$time_loca ...

  9. upc组队赛14 Evolution Game【dp】

    Evolution Game 题目描述 In the fantasy world of ICPC there are magical beasts. As they grow, these beast ...

  10. 使用FTP服务

    ftp 占用20   21两个端口 安装vsftpd程序 键入命令    yum install vsftpd -y 清空默认的防火墙默认规则: [root@linuxprobe ~]# iptabl ...