Codeforces 190E Counter Attack【思维+Bfs】好题!

来源:互联网 发布:淘宝小二介入买家输 编辑:程序博客网 时间:2024/05/18 00:33

E. Counter Attack
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland has managed to repel the flatlanders' attack and is now starting the counter attack.

Flatland has n cities, numbered from 1 to n, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.

The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!

Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them.

Input

The first line contains two space-separated integers n and m (1 ≤ n ≤ 5·105, 0 ≤ m ≤ 106) — the number of cities and the number of roads marked on the flatland map, correspondingly.

Next m lines contain descriptions of the cities on the map. The i-th line contains two integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers of cities that are connected by the i-th road on the flatland map.

It is guaranteed that each pair of cities occurs in the input no more than once.

Output

On the first line print number k — the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.

On each of the following k lines first print ti (1 ≤ ti ≤ n) — the number of vertexes in the i-th group. Then print space-separated numbers of cities in the i-th group.

The order of printing groups and the order of printing numbers in the groups does not matter. The total sum ti for all k groups must equal n.

Examples
input
4 41 21 34 24 3
output
22 1 4 2 2 3 
input
3 11 2
output
13 1 2 3 
Note

In the first sample there are roads only between pairs of cities 1-4 and 2-3.

In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3.


题目大意:


给出一个图,包含N个点和M条无向边,让你找出其补图的每个连通块的信息,要求输出每个联通块的大小,以及其中点的编号。


思路:


①如果原图中存在一条边x-y,那么补图中x和y才有可能不在同一个联通块中,反过去想,如果原图中不存在一条边x-y,那么补图中,x和y一定属于同一联通块。


②那么我们建立一个set,每次从中任取出来一个点x,先将x点丢入队列中,然后将set中剩余的点进行一次遍历,如果当前取出的点x和set中的某个点在原图中没有边相连,那么对应将这些点丢入队列中,而且我们能够知道,这些点一定在补图中都属于同一联通块。

依次遍历下去即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<vector>#include<algorithm>#include<queue>#include<set>using namespace std;set<int>st;vector<int>mp[500600],ans[500600];int del[500600];int n,m,tot;void Bfs(int x){    ans[tot].push_back(x);    st.erase(x);    queue<int>s;    s.push(x);    while(!s.empty())    {        int u=s.front();s.pop();        int cont=0;        for(set<int>::iterator it=st.begin();it!=st.end();it++)        {            int to=*it;            if(!binary_search(mp[u].begin(),mp[u].end(),to))            {                s.push(to);                ans[tot].push_back(to);                del[cont++]=to;            }        }        for(int i=0;i<cont;i++)st.erase(del[i]);    }    tot++;}int main(){    while(~scanf("%d%d",&n,&m))    {        tot=0;        st.clear();        for(int i=1;i<=n;i++)mp[i].clear(),ans[i].clear();        for(int i=1;i<=m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            mp[x].push_back(y);            mp[y].push_back(x);        }        for(int i=1;i<=n;i++)st.insert(i),sort(mp[i].begin(),mp[i].end());        for(int i=1;i<=n;i++)        {            if(st.count(i))Bfs(i);        }        printf("%d\n",tot);        for(int i=0;i<tot;i++)        {            printf("%d ",ans[i].size());            for(int j=0;j<ans[i].size();j++)            {                printf("%d ",ans[i][j]);            }            printf("\n");        }    }}












阅读全文
0 0
原创粉丝点击