# BZOJ1098 [POI2007]办公楼biu

## Code

#include <bits/stdc++.h>

using namespace std;

#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

int sum = 0, fg = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
return fg * sum;
}

const int maxn = 1e5 + 10;
const int maxm = 2e6 + 10;

int n, m;
int Begin[maxn], Next[maxm << 1], To[maxm << 1], e = -1;

inline void add(int x, int y) {
To[++e] = y, Next[e] = Begin[x], Begin[x] = e;
}

namespace DSU {
int fa[maxn];
void init() { for (int i = 1; i <= n + 1; i++) fa[i] = i; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) { x = find(x), y = find(y); if (x != y) fa[x] = y; }
}

bool v[maxn];
int cnt, ans[maxn];

void dfs(int now) {
v[now] = 1; ++cnt;
DSU::merge(now, now + 1);
for (int i = DSU::find(1), p = Begin[now]; i <= n; i = DSU::find(i + 1)) {
bool fg = 1;
for (; p + 1 && To[p] <= i; p = Next[p]) if (To[p] == i) { fg = 0; break; }
if (fg) dfs(i);
}
}

struct edge {
int x, y;
bool operator < (const edge &t) const { return x == t.x ? y > t.y : x > t.x; }
}eg[maxm];

int main() {
#ifdef xunzhen
freopen("set.in", "r", stdin);
freopen("set.out", "w", stdout);
#endif

memset(Begin, -1, sizeof Begin);
for (int i = 1; i <= m; i++) {
eg[i] = (edge){x, y};
}
sort(eg + 1, eg + m + 1);
for (int i = 1; i <= m; i++) add(eg[i].x, eg[i].y), add(eg[i].y, eg[i].x);

DSU::init();
for (int i = 1; i <= n; i++)
if (!v[i]) cnt = 0, dfs(i), ans[++ans[0]] = cnt;

printf("%d\n", ans[0]);
sort(ans + 1, ans + ans[0] + 1);
for (int i = 1; i <= ans[0]; i++) printf("%d%c", ans[i], i < ans[0] ? ' ' : '\n');

return 0;
}


## Summary

posted @ 2018-09-30 16:02  xunzhen  阅读(94)  评论(0编辑  收藏  举报