【题意】给一个无向图的反图(即给定的边实际上不存在,而未给的边是存在的),求连通块数。(点数n<=5*10^5,边数m<=10^6)

一开始我想的用DFS,枚举每一个点,跳过不存在的点,直接扫描,时间复杂度是O(n^2)的。然后,这种方案就被抛弃了。

我们可以设一个集合st,一开始st中包含所有点,每扫描到一个连通块,就把这个连通块从st中删掉。

那么怎么求连通块,可以使用BFS+二分查找。使用邻接表存储图,vector <int> G[i]为结点i相邻的结点,预处理时将其排序为有序序列。然后BFS,直接枚举st中的元素(即不存在已扫描到的连通块中的点),然后对该元素在G[i]中二分查找,如果没有找到,证明存在着这一条边,进行扩展即可。

时间复杂度:最坏情况下,所有点都没有边相连,二分查找O(logn),

【TIP】

  • 使用st代替了bool vis[i],避免重复无用的扫描。
  • 其次是stl二分查找的使用 binary_search(st.begin(),st.end(),elem):判断st中是否存在elem

  • 然后是图上这个地方,先erase(),再it++,程序会报错,然而先it++,再erase(),就能AC。。。我觉得应该是erase(v)操作,就是把v这个节点删除了,it之前是指向这个节点的,既然节点删除了,那么it就会指向一个很奇怪的地方,反正it++不一定再属于st。
#include<bits/stdc++.h>
#define eps 1e-9
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define MAXN 500005
#define MAXM 1000005
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
int i,j,k,n,m,x,y,T,ans,big,cas,num,len;
bool flag; vector <int> G[MAXN],out[MAXN];
set <int> st; void bfs(int u)
{
queue <int> q;
q.push(u); while (!q.empty())
{
u=q.front();
q.pop(); out[ans].push_back(u); for (set <int> ::iterator it=st.begin();it!=st.end();)
{
int v=*it;
it++;
if (!binary_search(G[u].begin(),G[u].end(),v))
{
q.push(v);
st.erase(v);
}
}
}
} int main()
{
scanf("%d%d",&n,&m); for (i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
} for (i=;i<=n;i++)
{
st.insert(i);
sort(G[i].begin(),G[i].end());
} ans=;
while (!st.empty())
{
int u=*(st.begin());
st.erase(u);
ans++;
bfs(u);
} printf("%d\n",ans);
for (i=;i<=ans;i++)
{
int size=out[i].size();
printf("%d ",size);
for (j=;j<size-;j++)
{
printf("%d ",out[i][j]);
}
printf("%d\n",out[i][size-]);
} return ;
}

Codeforces 190E - Counter Attack的更多相关文章

  1. Codeforces Round #120 (Div. 2)

    A. Vasya and the Bus 根据\(n,m\)是否为0分类讨论下. B. Surrounded 判断两圆是否有交点,否则构造的圆与两圆相切. C. STL 看代码比较清楚. void t ...

  2. XSS CSS Cross SiteScript 跨站脚本攻击

    XSS攻击及防御 - 高爽|Coder - CSDN博客 https://blog.csdn.net/ghsau/article/details/17027893 XSS又称CSS,全称Cross S ...

  3. codeforces 495A. Digital Counter 解题报告

    题目链接:http://codeforces.com/problemset/problem/495/A 这个题目意思好绕好绕~~好绕~~~~~,昨天早上做得 virtual 看不懂,晚上继续看还是,差 ...

  4. Codeforces Round #115 A. Robot Bicorn Attack 暴力

    A. Robot Bicorn Attack Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/17 ...

  5. Codeforces Round #364 (Div. 2) B. Cells Not Under Attack

    B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  6. CodeForces 701B Cells Not Under Attack

    题目链接:http://codeforces.com/problemset/problem/701/B 题目大意: 输入一个数n,m, 生成n*n的矩阵,用户输入m个点的位置,该点会影响该行和该列,每 ...

  7. Codeforces Round #364 (Div. 2) Cells Not Under Attack

    Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...

  8. codeforces #364b Cells Not Under Attack

    比赛的时候 long long sum=n*n,计算不出1e10长度到数,没有搞掉. 哎,以后要注意这个地方.这个题其实不难: 统计能被攻击到的个数,然后用总的个数减掉就可以了.注意有些地方重复计算, ...

  9. codeforces 701B B. Cells Not Under Attack(水题)

    题目链接: B. Cells Not Under Attack 题意: n*n的棋盘,现在放m个棋子,放一个棋子这一行和这一列就不会under attack了,每次放棋子回答有多少点还可能under ...

随机推荐

  1. display:inline 跟 display:block 跟 display:inline-block区别

    我来说句人话吧.display:inline; 内联元素,简单来说就是在同一行显示.display:block; 块级元素,简单来说就是就是有换行,会换到第二行.display:inline-bloc ...

  2. php函数——『解析 xml数据』

    <?php //该文件是 //$raw_post_data = file_get_contents('php://input'); //file_put_contents('a.txt', $r ...

  3. Flask jQuery ajax

    http://www.runoob.com/jquery/jquery-ref-ajax.html http://jun1986.iteye.com/blog/1399242 下面是jQuery官方给 ...

  4. PHP要注意的14个问题

    1.页面之间无法传递变量 get,post,session 在最新的php版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使 用$_GET['foo'],$_POST['foo'],$ ...

  5. C语言学习笔记(二):指针的用法

    与其说指针是一种工具,不如先说指针是一种数据类型. -------------------------------------------------------------华丽的分割线------- ...

  6. string.Format 日期格式化

    String.Format日期的格式化例子: DateTime dt = DateTime.Now;//2010年10月4日 17点05分 string str = ""; //s ...

  7. 重构oceanbase的一个函数

    我去,今天读了一下ob的源码,感觉有点乱啊!!!好吧,当作练手,我重构了一个函数 void* ObMySQLCallback::decode(easy_message_t* m) { uint32_t ...

  8. java interface

  9. 不管,干一个HAPROXY先

    说不定要用到的塞.... 1.wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.12.tar.gz 2.tar -zxvf haprox ...

  10. Java Integer类分析

    public static final int   MIN_VALUE = 0x80000000;  -2^31 public static final int   MAX_VALUE = 0x7ff ...