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

本文介绍了一种高效算法,用于求解给定图的补图中的所有连通块,并详细解释了实现步骤及核心思想。

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 4
1 2
1 3
4 2
4 3
output
2
2 1 4 
2 2 3 
input
3 1
1 2
output
1
3 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");
        }
    }
}












### Codeforces 思维思路和技巧 #### 预处理的重要性 对于许多竞赛编程问而言,预处理能够显著提高效率并简化后续操作。通过提前计算某些固定的数据结构或模式匹配表,可以在实际求解过程中节省大量时间。例如,在字符串处理类目中预先构建哈希表来加速查找过程[^1]。 #### 算法优化策略 针对特定类型的输入数据设计高效的解决方案至关重要。当面对大规模测试案例时,简单的暴力破解往往无法满足时限要求;此时则需考虑更高级别的算法改进措施,比如动态规划、贪心算法或是图论中的最短路径算法等。此外,合理利用空间换取时间也是一种常见的优化手段[^2]。 #### STL库的应用价值 C++标准模板库提供了丰富的容器类型(vector, deque)、关联式容器(set,map)以及各种迭代器支持,极大地便利了程序开发工作。熟练掌握这些工具不仅有助于快速实现功能模块,还能有效减少代码量从而降低出错几率。特别是在涉及频繁插入删除场景下,优先选用双向队列deque而非单向链表list可获得更好的性能表现。 ```cpp #include <iostream> #include <deque> using namespace std; int main(){ deque<int> dq; // 向两端添加元素 dq.push_back(5); dq.push_front(3); cout << "Front element is: " << dq.front() << endl; cout << "Back element is : " << dq.back() << endl; return 0; } ``` #### 实际应用实例分析 以一道具体目为例:给定一系列查询指令,分别表示往左端/右端插入数值或者是询问某个指定位置到边界之间的最小距离。此目的关键在于如何高效地追踪最新状态而无需重复更新整个数组。采用双指针技术配合静态分配的一维数组即可轻松解决上述需求,同时保证O(n)级别的总运行成本[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值