BZOJ1098: [POI2007]办公楼biu(补图连通块)

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

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

Source

同https://blog.csdn.net/tomjobs/article/details/104294752

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn = 2e5 + 7;
vector<int>G[maxn];
int vis[maxn],uncn[maxn],ans[maxn];
int lst[maxn],nex[maxn];
queue<int>Q;

void del(int x)
{
    nex[lst[x]] = nex[x];lst[nex[x]] = lst[x];
}

int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        G[x].push_back(y);G[y].push_back(x);
    }
    for(int i = 1;i < n;i++)
    {
        nex[i] = i + 1;lst[i + 1] = i;
    }
    nex[0] = 1;
    
    int cnt = 0;
    for(int i = 1;i <= n;i++)
    {
        if(vis[i] == 0)
        {
            ans[++cnt] = 1;vis[i] = 1;
            Q.push(i);del(i);
            while(!Q.empty())
            {
                int now = Q.front();Q.pop();
                for(int j = 0;j < G[now].size();j++)
                {
                    int v = G[now][j];
                    if(vis[v] == 0)uncn[v] = 1;
                }
                for(int j = nex[0];j;j = nex[j])
                {
                    if(uncn[j] == 0)
                    {
                        vis[j] = 1;ans[cnt]++;del(j);
                        Q.push(j);
                    }
                    else uncn[j] = 0;
                }
            }
        }
    }
    sort(ans + 1,ans + 1 + cnt);
    printf("%d\n",cnt);
    for(int i = 1;i <= cnt;i++)printf("%d ",ans[i]);
    return 0;
}



发布了697 篇原创文章 · 获赞 22 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104295216