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 & ...
随机推荐
- boost库:函数对象
函数对象是指那些可以被传入到其它函数或是从其它函数返回的一类函数. 1. boost::bind bind提供了一个机制,是函数与几乎不限数量的参数一起使用,就可以得到指定签名的函数.bind会复制传 ...
- 【多线程】LinkedTransferQueue
LinkedTransferQueue是JDK1.7才添加的阻塞队列,基于链表实现的FIFO无界阻塞队列,是ConcurrentLinkedQueue(循环CAS+volatile 实现的wait-f ...
- js的 算法 和 数据结构
js的 算法 1.对一个对象数组按照对象某个属性进行排序 : https://www.cnblogs.com/webcabana/p/7460038.html 在做公交的项目中就碰到过这种算法问题, ...
- mysql负载高分析
table_open_cache SHOW STATUS LIKE 'Open%tables'; SHOW global variables LIKE '%table%'; # 如果你发现 op ...
- [六省联考2017]分手是祝愿 题解(期望dp)
题目描述 B 君在玩一个游戏,这个游戏由 n 个灯和 n 个开关组成,给定这 n 个灯的初始状态,下标为从 1 到 n 的正整数. 每个灯有两个状态亮和灭,我们用 1 来表示这个灯是亮的,用 0 表示 ...
- 用Node编写RESTful API接口
前言 本文将通过一个todo list前后端分离的小项目来讲解如何用Node创建符合RESTful风格的API接口. 创建HTTP服务器 我们先来了解下如何用Node创建HTTP服务器(熟悉的读者可以 ...
- java并发编程笔记(二)——并发工具
java并发编程笔记(二)--并发工具 工具: Postman:http请求模拟工具 Apache Bench(AB):Apache附带的工具,测试网站性能 JMeter:Apache组织开发的压力测 ...
- 12-python基础—python3中的reduce()
在 Python3 中,reduce() 函数已经被从全局名字空间里移除了,它现在被放置在 functools 模块里,需要通过引入 functools 模块来调用 reduce() 函数: from ...
- 两分钟学会Unity3D布娃娃的使用
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangbin_jxust/article/details/28587233 在RPG游戏中,为了让人 ...
- 利用单选框的单选特性作tab切换
<RadioGroup v-model="selectType" type="button" @onchange="selectTypeChan ...