[POI 2007] 办公楼
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1098
[算法]
显然 , 答案为补图的连通分量个数
用链表优化BFS , 时间复杂度 : O(N + M)
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
#define MAXM 2000010 struct edge
{
int to , nxt;
} e[MAXM << ]; int n , m , tot , cnt;
int head[MAXN] , ans[MAXN] , L[MAXN] , R[MAXN];
bool visited[MAXN] , mark[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v)
{
++tot;
e[tot] = (edge){v , head[u]};
head[u] = tot;
}
inline void del(int x)
{
R[L[x]] = R[x];
L[R[x]] = L[x];
}
inline void bfs(int s)
{
queue< int > q;
visited[s] = true;
q.push(s);
while (!q.empty())
{
int cur = q.front();
q.pop();
++ans[cnt];
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to;
mark[v] = true;
}
for (int i = R[]; i; i = R[i])
{
if (!mark[i] && !visited[i])
{
visited[i] = true;
del(i);
q.push(i);
}
}
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to;
mark[v] = false;
}
}
} int main()
{ read(n); read(m);
for (int i = ; i <= m; i++)
{
int u , v;
read(u); read(v);
addedge(u , v);
addedge(v , u);
}
for (int i = ; i <= n; i++) L[i] = i - , R[i] = i + ;
R[n] = ;
for (int i = ; i <= n; i++)
{
if (!visited[i])
{
++cnt;
bfs(i);
}
}
printf("%d\n" , cnt);
sort(ans + , ans + cnt + );
for (int i = ; i <= cnt; i++) printf("%d " , ans[i]);
printf("\n"); return ; }
[POI 2007] 办公楼的更多相关文章
- [POI 2007]ZAP-Queries
Description Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency) ...
- 解题:POI 2007 Tourist Attractions
题面 事实上这份代码在洛谷过不去,因为好像要用到一些压缩空间的技巧,我并不想(hui)写(捂脸) 先预处理$1$到$k+1$这些点之间相互的最短路和它们到终点的最短路,并记录下每个点能够转移到时的状态 ...
- 解题:POI 2007 Driving Exam
题面 有点意思的题 从一个位置$i$出发可以到达每一个位置即是从$1,n$出发可以到达$i$.然后有了一个做法:把图上下反转后建反图,这样就可以求从一个点$i$到达左右两侧的花费$dp[i][0/1] ...
- 解题:POI 2007 Weights
题面 这是个$O(nlog^2$ $n)$的解法,因为蒟蒻博主没有看懂$O(nlog$ $n)$的更优秀的解法 显然从小到大装砝码是最优的方法,又显然从大到小装容器不会使得答案变劣,还显然砝码数具有单 ...
- [POI 2007] Zap
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1101 [算法] 首先 , 问题可以转化为求GCD(x,y) = 1,x <= ...
- BZOJ 1101 Luogu P3455 POI 2007 Zap (莫比乌斯反演+数论分块)
手动博客搬家: 本文发表于20171216 13:34:20, 原地址https://blog.csdn.net/suncongbo/article/details/78819470 URL: (Lu ...
- [POI 2007] 堆积木
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1109 [算法] DP [代码] #include<bits/stdc++.h& ...
- 【POI 2007】 山峰和山谷
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1102 [算法] 广度优先搜索 [代码] #include<bits/stdc+ ...
- [POI 2007] 旅游景点
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1097 [算法] 首先,用Dijkstra算法求出2-k+1到每个点的最短路 然后,我 ...
随机推荐
- centos7 安装teamviewer 报错libQt5WebKitWidgets.so.5()(64bit)
https://blog.csdn.net/kenny_lz/article/details/78884603
- T2602 最短路径问题 codevs
http://codevs.cn/problem/2602/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 平面上有n个点(n& ...
- springboot启动报异常,Failed to load property source from location 'classpath:/application.yml'
学习springboot,在启动时抛出下图异常 往下看异常信息,找到异常的具体位置 找到application.yml文件的对应位置,发现params配置前面多了空格 去掉空格重新启动,可以了 写代码 ...
- Java生成读取条形码和二维码图片
原文:http://www.open-open.com/code/view/1453520722495 package zxing; import com.google.zxing.BarcodeFo ...
- Mathematica 表达式求值
表达式是变量之间的运算关系.表达式求值就是对变量赋值并运算出结果的过程. 针对于Mathematica中的表达式.有两种方法获得其值. 一是对对应的变量直接赋值.这样对应的表达式在调用时便会直接运算得 ...
- 上传jar包至maven私服
1.maven环境变量配置(新建系统变量,编辑Path) 2.修改maven的setting文件 2.1 私服的用户配置 2.2 私服镜像配置 2.3 (我也不知道是啥) <profile> ...
- HDU2897( 巴什博奕变形)
邂逅明下 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- [ACM] ZOJ 3725 Painting Storages (DP计数+组合)
Painting Storages Time Limit: 2 Seconds Memory Limit: 65536 KB There is a straight highway with ...
- struts开发<在eclipse中配置struts. 一>
1.获取struts的jar包 1.1首先在http://struts.apache.org/download.cgi#struts23163这里下载 struts的文件包(选择struts-2.3. ...
- 一致性hash算法在memcached中的使用
一.概述 1.我们的memcacheclient(这里我看的spymemcache的源代码).使用了一致性hash算法ketama进行数据存储节点的选择.与常规的hash算法思路不同.仅仅是对我们要存 ...