Codeforces 190E Counter Attack【思维+Bfs】好题!
来源:互联网 发布:淘宝小二介入买家输 编辑:程序博客网 时间:2024/05/18 00:33
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.
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.
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.
4 41 21 34 24 3
22 1 4 2 2 3
3 11 2
13 1 2 3
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 190E Counter Attack【思维+Bfs】好题!
- 【Codeforces】-701B-Cells Not Under Attack(思维,好)
- Codeforces 586D Phillip and Trains【思维+Bfs】好题~!
- Codeforces 584E Anton and Ira【思维+贪心】好题~
- Codeforces 591E Three States【优先队列Bfs+思维】
- CodeForces 701B Cells Not Under Attack 思维题
- Codeforces 369E Valera and Queries【逆向思维+离线+树状数组】好题!好题!好题!
- Codeforces 426E Sereja and Two Sequences【思维+Dp+二分】好题!
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- Codeforces 876E National Property【思维建图+2-sat+输出可行解】好题~
- Codeforces Round #397 E. Tree Fold(bfs,想法题,好题)
- Codeforces 509E(思维)
- 南邮NOJ Counter Attack
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!
- Codeforces 353D Queue【思维】好题~
- Codeforces 276E Little Girl and Problem on Trees【线段树+Bfs序+Dfs序】好题!
- cf 120 c,d,e STL Non-Secret Cypher Counter Attack
- ZOJ-2849-Attack of Panda Virus【bfs】【优先级队列】【4th浙江省赛】【好题】
- 不可变类
- List中的泛型
- 洛谷 P2290 [HNOI2004]树的计数(bzoj[1211])
- ubuntu libnice编译测试
- hannnnah_j’s Biological Test HDU
- Codeforces 190E Counter Attack【思维+Bfs】好题!
- CentOS7使用firewalld打开关闭防火墙与端口
- 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
- angularJS 路由不能正常工作 URL中出现#!
- 归并排序
- 心情随笔
- 正则表达式小结
- java中的线程、进程、锁
- tomcat 优化