Codeforces 190E - Counter Attack

【题意】给一个无向图的反图(即给定的边实际上不存在,而未给的边是存在的),求连通块数。(点数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=1;i<=m;i++)
    {
        scanf("%d %d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }

    for (i=1;i<=n;i++)
    {
        st.insert(i);
        sort(G[i].begin(),G[i].end());
    }

    ans=0;
    while (!st.empty())
    {
        int u=*(st.begin());
        st.erase(u);
        ans++;
        bfs(u);
    }

    printf("%d\n",ans);
    for (i=1;i<=ans;i++)
    {
        int size=out[i].size();
        printf("%d ",size);
        for (j=0;j<size-1;j++)
        {
            printf("%d ",out[i][j]);
        }
        printf("%d\n",out[i][size-1]);
    }

    return 0;
}
时间: 2024-11-08 12:07:32

Codeforces 190E - Counter Attack的相关文章

codeforces 495A. Digital Counter 解题报告

题目链接:http://codeforces.com/problemset/problem/495/A 这个题目意思好绕好绕~~好绕~~~~~,昨天早上做得 virtual 看不懂,晚上继续看还是,差点就想求救 XX 兽了,最终还是打住,尽量不要依赖人嘛.今天终于想到了,大感动 ~_~ 理解能力有待提高... One of the sticks of the counter was broken    这句话有一点误导人的成分,我刚开始就以为只有一条 stick 坏了= =,再看这句 becau

CodeForces 1292C Xenon&#39;s Attack on the Gangs

Description 描述 给一个 $n$ 个点的树,要求你将 $0 \sim n - 2$ 不重不漏的放在这 $n - 1$ 条边上,求 $S = \sum\limits_{1 \le u < v \le n} \operatorname{mex}(u, v)$ 的最大值,$\operatorname{mex}(u, v)$ 表示 $<\! u \to v \!>$ 的路径上所经过的边权集合中最小的没出现的非负数. 输入 第一行一个正整数 $n$($2 \le n \le 3000$

Codeforces Round #614 (Div. 2) E. Xenon&#39;s Attack on the Gangs

On another floor of the A.R.C. Markland-N, the young man Simon "Xenon" Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker "X" instinct and fight ag

Codeforces 701B. Cells Not Under Attack

Vasya has the square chessboard of size n?×?n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another. The cell of the field is under rook's attack, if there is at least one rook located in

codeforces #364b Cells Not Under Attack

比赛的时候 long long sum=n*n,计算不出1e10长度到数,没有搞掉. 哎,以后要注意这个地方.这个题其实不难: 统计能被攻击到的个数,然后用总的个数减掉就可以了.注意有些地方重复计算,要给去掉. B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Vasya ha

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

题目链接: B. Cells Not Under Attack 题意: n*n的棋盘,现在放m个棋子,放一个棋子这一行和这一列就不会under attack了,每次放棋子回答有多少点还可能under attack; 思路:对行和列标记; AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #incl

Codeforces 839D Winter is here - 暴力 - 容斥原理

Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a m

Identifying a distributed denial of service (DDOS) attack within a network and defending against such an attack

The invention provides methods, apparatus and systems for detecting distributed denial of service (DDoS) attacks within the Internet by sampling packets at a point or points in Internet backbone connections to determine a packet metric parameter. The

CodeForces 12C Fruits

Fruits Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 12C Description The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera deci