[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到每个点的最短路 然后,我 ...
随机推荐
- POJ 3694 (tarjan缩点+LCA+并查集)
好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...
- P2136 拉近距离(spfa判负环)
洛谷—— P2136 拉近距离 题目背景 我是源点,你是终点.我们之间有负权环. ——小明 题目描述 在小明和小红的生活中,有N个关键的节点.有M个事件,记为一个三元组(Si,Ti,Wi),表示从节点 ...
- scanf,fscanf,sscanf的区别----整理
转自原文 scanf,fscanf,sscanf的区别----整理 scanf 从控制台输入 fscanf 从文件输入 sscanf 从指定字符串输入 1.例:使用scanf函数输入数据. #incl ...
- setImageEdgeInsets 和 setImage配合使用达到button区域大并可调节其上图片显示区域大小的效果
[self.indicator setImage:[UIImage imageNamed:@"01_login_moreicon@2x.png"] forState:UIContr ...
- Linux C函数库大全
(1)字符测试函数 isalnum(测试字符是否为英文字母或数字) isalpha(测试字符是否为英文字母) isascii(测试字符是否为ASCII码字符) isblank(测试字符是否为空格字符) ...
- windows10 開機失敗,且按F8無法進入安全模式
windows10 開機失敗,且按F8無法進入安全模式: 在cmd視窗下: bcdedit set {default} bootmenupolicy legacy 重啟,再按F8試一試吧! To En ...
- OCP47:155
- linux字符设备驱动程序框架(老方法)
#include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #inclu ...
- ucgui界面设计演示样例2
ucgui界面设计演示样例2 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:WIN8 开发环境:MDK4.72 ucgui版本号:3 ...
- CPU维修技术
中央处理单元(Central Process Unit)简称CPU,是电脑的核心部件,负责处理和运算电脑内部所有数据.因其在电脑中的地位相当重要,所以一旦发生故障就会造成很严重的后果. [技术66]开 ...