【BZOJ】1098: [POI2007]办公楼biu(补图+bfs+链表)

http://www.lydsy.com/JudgeOnline/problem.php?id=1098

显然答案是补图连通块。。。。。

想到用并查集。。。可是连补图的边都已经。。。n^2了。。。怎么做。。

好神的题!

考虑我们是如何建补图和并查集:是不是都是先枚举一个点,然后标记能到达的点,然后没到达的点就是要连的边。最后在来一次并查集。

是吧。。

那么考虑:为什么要并查集。。。我们找到没到达的点就行了么。。。那么肯定就是在一个连通块,问题在于如何将整个连通块找出来。bfs!

考虑:我们之前放到块里的点是不用枚举了的,且在标记的时候,已经不必枚举之前已经得到的连通块的点。也就是说,每一次找到一个块,就将点删除,这样找标记就不必要枚举这些点!

bfs+链表的结构是不是已经出来了。。。。。

链表用于遍历所有还没用变成连通块的点,bfs用于找出连通块。

然后就没了。

orz

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }

const int N=100005;
int ihead[N], cnt, n, m, q[N], ans[N], tot, vis[N];
struct dat { int next, to; }e[2000005*2];
struct node { int nxt; bool flag; }c[N];
void add(int u, int v) {
	e[++cnt].next=ihead[u]; ihead[u]=cnt; e[cnt].to=v;
	e[++cnt].next=ihead[v]; ihead[v]=cnt; e[cnt].to=u;
}
inline void del(const int &x, const int &f) { c[f].nxt=c[x].nxt; }
void work() {
	for1(i, 1, n) c[i-1].nxt=i;
	int front, tail, pos, f, u;
	while(c[0].nxt) {
		front=tail=0;
		q[tail++]=c[0].nxt; vis[c[0].nxt]=1; ans[tot]=1; del(c[0].nxt, 0);
		while(front!=tail) {
			u=q[front++];
			rdm(u, i) c[e[i].to].flag=1;
			for(pos=c[0].nxt, f=0; pos; pos=c[pos].nxt) if(!c[pos].flag) { if(vis[pos]) continue; vis[pos]=1; del(pos, f); ++ans[tot]; q[tail++]=pos; } else f=pos;
			rdm(u, i) c[e[i].to].flag=0;
		}
		++tot;
	}
}
int main() {
	read(n); read(m);
	for1(i, 1, m) add(getint(), getint());
	work();
	printf("%d\n", tot);
	sort(ans, ans+tot);
	rep(i, tot) printf("%d ", ans[i]);
	return 0;
}

  

 


 

 

Description

FGD开办了一家电话公司。他雇用了N个职员,给了每个职员一部手机。每个职员的手机里都存储有一些同事的电话号码。由于FGD的公司规模不断扩大,旧的办公楼已经显得十分狭窄,FGD决定将公司迁至一些新的办公楼。 FGD希望职员被安置在尽量多的办公楼当中,这样对于每个职员来说都会有一个相对更好的工作环境。但是,为了联系方便起见,如果两个职员被安置在两个不同的办公楼之内,他们必须拥有彼此的电话号码。

Input

第一行包含两个整数N(2<=N<=100000)和M(1<=M<=2000000)。职员被依次编号为1,2,……,N. 以下M行,每行包含两个正数A和B(1<=A

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号办公楼。

 

Source

 

 
posted @ 2014-12-13 12:16  iwtwiioi  阅读(610)  评论(0编辑  收藏  举报