[POI2007]办公楼biu
2024-09-08 08:09:53
Description
FGD开办了一家电话公司。他雇用了N个职员,给了每个职员一部手机。每个职员的手机里都存储有一些同事的电话号码。由于FGD的公司规模不断扩大,旧的办公楼已经显得十分狭窄,FGD决定将公司迁至一些新的办公楼。FGD希望职员被安置在尽量多的办公楼当中,这样对于每个职员来说都会有一个相对更好的工作环境。但是,为了联系方便起见,如果两个职员被安置在两个不同的办公楼之内,他们必须拥有彼此的电话号码。
Input
第一行包含两个整数N(2<=N<=100000)和M(1<=M<=2000000)。职员被依次编号为1,2,……,N.以下M行,每行包含两个正数A和B(1<=A<b<=n),表示职员a和b拥有彼此的电话号码),li <= 1000
Output
包含两行。第一行包含一个数S,表示FGD最多可以将职员安置进的办公楼数。第二行包含S个从小到大排列的数,每个数后面接一个空格,表示每个办公楼里安排的职员数。
Sample Input
7 16
1 3
1 4
1 5
2 3
3 4
4 5
4 7
4 6
5 6
6 7
2 4
2 7
2 5
3 5
3 7
1 7
Sample Output
3
1 2 4
HINT
FGD可以将职员4安排进一号办公楼,职员5和职员7安排进2号办公楼,其他人进3号办公楼。
首先这题很容易想到求补图的联通块个数,然后就想着大力bfs,对于每个点,将它不能到达的点拓展进队列,知道不能拓展为止。
然后,T掉……
为什么会T呢,主要是因为这句话
for (int i=1;i<=n;i++) if (不联通) 进入队列;
每次O(n)枚举,这是非常不优的。所以我们考虑用一个链表来存,每次进队的点就从链表中删去,这样就可以保证进队的点不会被枚举。
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e5,M=2e6;
int pre[(M<<1)+10],now[N+10],child[(M<<1)+10],tot;
int h[N+10],size[N+10],Next[N+10],Pre[N+10],vis[N+10],dfn,Time;
void join(int x,int y){pre[++tot]=now[x],now[x]=tot,child[tot]=y;}
void clear(int x){
Next[Pre[x]]=Next[x];
Pre[Next[x]]=Pre[x];
}
int main(){
int n=read(),m=read();
for (int i=1;i<=m;i++){
int x=read(),y=read();
join(x,y),join(y,x);
}
for (int i=0;i<=n;i++) Pre[Next[i]=i+1]=i;
while (Next[0]!=n+1){
int head=1,tail=1,cnt=1;
h[1]=Next[0];
clear(h[1]);
for (;head<=tail;head++){
int Now=h[head];Time++;
for (int p=now[Now],son=child[p];p;p=pre[p],son=child[p]) vis[son]=Time;
for (int i=Next[0];i!=n+1;i=Next[i]) if (vis[i]!=Time) h[++tail]=i,clear(i),cnt++;
//链表枚举优化
}
size[++dfn]=cnt;
}
sort(size+1,size+1+dfn);
printf("%d\n",dfn);
for (int i=1;i<=dfn;i++) printf("%d",size[i]),i!=dfn?putchar(' '):putchar('\n');
return 0;
}
最新文章
- PHP高级——抽象类与接口的区别(转)
- php 克隆和引用类
- shell常用代码
- CodeMIrror 简单使用
- 插入排序与shell排序(希尔排序)
- ubuntu 彻底删除软件包
- KMP模式匹配 三(弦)
- away 3d的一些问题
- time_wait和clost_wait说明
- 花店橱窗(flower)
- centos7.4 搭建zabbix-server 3.4.5
- 更换yum源
- python对象的多重继承
- Two strings 的另一种解法
- webapi 405 method not allowed
- flex-direction
- _rqy&#39;s Code Style for OI
- tomcat访问
- python面试题(转自https://www.cnblogs.com/wupeiqi/p/9078770.html)
- 频繁项集挖掘之Aprior和FPGrowth算法
热门文章
- office outlook 無法開啟 outlook 視窗
- hdoj 3351 Seinfeld 【栈的简单应用】
- Sql Server 导入还有一个数据库中的表数据
- hdu4941 Magical Forest
- 使用7zip压解各种文件的经常使用命令
- Java使用三种不同循环结构对1+2+3+...+100 求和
- java读取Excel表格中的数据
- php截取某二个特殊字符串间的某段字符串
- Fluently NHibernate 插入CLOB字段
- atom及其插件activate-power-mode下载安装