---
id: 1707
lang: en
translation_of: index.md
source_sha256: 525e8c32bf3c3965cc88db84c71c756df27d14e8a51eb010e3ebc72a8fcf61f5
title: "[POI2007] BIU-Offices — Solution"
description: "Vertices in a connected component of the complement graph are connected in the original graph, so build the complement graph and run a BFS."
tags: ["OI","Graph Theory","Complement Graph"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "反图 here means the complement graph, so the prose renders it as “complement graph”; the tag keeps the tags.yaml form “Reverse Graph”."
  - "Book title 《组合数学》 rendered as “Combinatorics” in curly quotes, as in post 668."
  - "联通 (a variant spelling of 连通) translated as “connected”, same as 连通."
---

## Main Idea

I happened to have seen a result in “Combinatorics”: the vertices of a connected component formed in the complement graph are certainly connected everywhere in the original graph. The proof is simple: consider a vertex pair $(u, v)$. If the pair is not connected in the complement graph, then obviously it is connected in the original graph; if it is connected in the complement graph, there is a vertex not in the current vertex set that connects them. So we only need to build the complement graph and run a BFS based on “every vertex not adjacent to this vertex is in this connected component”.

<!--more-->

## Code

```cpp
// P3452.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200, MAX_E = 4e6 + 200;

int head[MAX_N], current, n, m;

struct edge
{
    int to, nxt;
} edges[MAX_E];

bool vis[MAX_N], ext[MAX_N];

vector<int> pool;

void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}

int bfs(int x)
{
    int tot = 0;
    queue<int> q;
    q.push(x), ext[x] = true;
    while (!q.empty())
    {
        int u = q.front();
        q.pop(), tot++;
        vis[u] = true;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            ext[edges[i].to] = true;
        vector<int> tmp = pool;
        pool.clear();
        for (int i = 0, siz = tmp.size(); i < siz; i++)
            if (ext[tmp[i]])
                pool.push_back(tmp[i]);
            else
                q.push(tmp[i]);
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            ext[edges[i].to] = false;
    }
    return tot;
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v; i <= m; i++)
        scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
    for (int i = 1; i <= n; i++)
        pool.push_back(i);
    vector<int> ans;
    for (int i = 1; i <= n; i++)
        if (vis[i] == false)
            ans.push_back(bfs(i));
    sort(ans.begin(), ans.end());
    printf("%d\n", ans.size());
    for (int i = 0, siz = ans.size(); i < siz; i++)
        printf("%d ", ans[i]);
    return 0;
}
```
