bzoj1098题解
【题意分析】
给你一张无向图,求其补图的联通块数及各个联通块大小。
【解题思路】
暴搜!
然而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题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- zTree节点排序、jsTree节点排序
项目中遇到了这个问题,网上也没找到比较清晰的答案,索性提供一个方案吧. 原理:将整个树形插件的数据源进行排序,插件在构造UI时,自然也是按照顺序来排列的,目前这种思路适用于 zTree 和 jsTre ...
- (转)MyEclipse中使用git
转:https://www.jianshu.com/p/92ee5c99d3a8 Myeclipse老版本可能需要安装一个插件,高版本中已经安装好了. 连接github 当然我们之前已经有仓库了,我们 ...
- idea 使用github
[Toc] #一.首先下载github for window 客户端,或者git客户端 这里只演示gitHub客户端,安装git客户端的话,git.exe很容易找得到. 附上网址:https://de ...
- js判断是否pc端
function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = ['Android', 'iPhone', 'Symbi ...
- 02 java语言基础
常量:字面值常量(字符串,字符,整数,小数,布尔,null),自定义常量,''这个不是字符常量,""这个是字符串常量 进制: 02.01_Java语言基础(常量的概述和使用) A: ...
- %matplotlib inline 被注释掉后,pycharm不能生成图
目录 问题描述 解决方案 @ 问题描述 在 jupyter 编译器中 程序的开头,有这么一行 %matplotlib inline import numpy as np import matplotl ...
- Codeforces 388C Fox and Card Game (贪心博弈)
Codeforces Round #228 (Div. 1) 题目链接:C. Fox and Card Game Fox Ciel is playing a card game with her fr ...
- ROS编程: 重要的代码优化知识点记录(1)
订阅多个话题并对其进行同步处理 本小节针对在ROS节点中需要订阅两个及两个以上的话题时,需要保持对这两个话题数据的同步,且需要同时接收数据一起处理然后当做参数传入到另一个函数中: 研究背景:reals ...
- 爬虫(五)—— selenium模块启动浏览器自动化测试
目录 selenium模块 一.selenium介绍 二.环境搭建 三.使用selenium模块 1.使用chrome并设置为无GUI模式 2.使用chrome有GUI模式 3.查找元素 4.获取标签 ...
- MYSQL索引的深入学习
通常大型网站单日就可能会产生几十万甚至几百万的数据,对于没有索引的表,单表查询可能几十万数据就是瓶颈. 一个简单的对比测试 以我去年测试的数据作为一个简单示例,20多条数据源随机生成200万条数据,平 ...