[BZOJ 1098] [POI2007] 办公楼biu 【链表优化BFS】

代码

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <queue>

using namespace std;

const int MaxN = 100000 + 5, MaxM = 2000000 + 5;

{
char c; c = getchar();
while (c < '0' || c > '9') c = getchar();
Num = c - '0'; c = getchar();
while (c >= '0' && c <= '9')
{
Num = Num * 10 + c - '0';
c = getchar();
}
}

int n, m, Top, R1, R2, Sum;
int Ans[MaxN], Last[MaxN], Next[MaxN];

bool InList[MaxN];

struct Edge
{
int v;
Edge *Next;
} E[MaxM * 2], *P = E, *Point[MaxN];

inline void AddEdge(int x, int y)
{
++P; P -> v = y;
P -> Next = Point[x]; Point[x] = P;
}

queue<int> Q;

inline void Add(int x, int y)
{
Next[y] = Next[x];
if (Next[x]) Last[Next[x]] = y;
Next[x] = y;
Last[y] = x;
}

inline void Delete(int x)
{
if (Last[x]) Next[Last[x]] = Next[x];
if (Next[x]) Last[Next[x]] = Last[x];
}

void BFS()
{
while (!Q.empty()) Q.pop();
Q.push(Next[R1]);
InList[Next[R1]] = false;
Delete(Next[R1]);
int x, y;
++Top;
while (!Q.empty())
{
x = Q.front();
++Sum;
++Ans[Top];
Q.pop();
R2 = n + 2;
Last[R2] = Next[R2] = 0;
for (Edge *j = Point[x]; j; j = j -> Next)
{
y = j -> v;
if (!InList[y]) continue;
Delete(y);
}
for (int i = Next[R1]; i; i = Next[i])
{
Q.push(i);
InList[i] = false;
}
Next[R1] = Next[R2];
Last[Next[R1]] = R1;
}
}

int main()
{
scanf("%d%d", &n, &m);
int a, b;
for (int i = 1; i <= m; ++i)
{
}
Top = 0; Sum = 0;
R1 = n + 1;
for (int i = 1; i <= n; ++i) Add(R1, i);
for (int i = 1; i <= n; ++i) InList[i] = true;
while (Sum < n) BFS();
printf("%d\n", Top);
sort(Ans + 1, Ans + Top + 1);
for (int i = 1; i <= Top; ++i) printf("%d ", Ans[i]);
return 0;
}


posted @ 2015-03-09 09:07  JoeFan  阅读(486)  评论(0编辑  收藏  举报