P1208 犯罪团伙(超大数据)

P1208 犯罪团伙(超大数据)
2017年5月16日

BFS+邻接表

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

const int num_max = 51000;
const int tion_max = 400400;
int N, M;
int ans_groups = 0;

struct CRIMINAL_LINES{
    int y, next;
}K[tion_max];
bool Jud[num_max];
int vis[num_max];
int len = 0;

int quens[num_max];
int tails = 1;

inline void putin(int xx, int yy)
{
    len++;
    K[len].y = yy;
    K[len].next = vis[xx];
    vis[xx] = len;
    return;
}

void Insert()
{
    cin >> N >> M;
    memset(K, 0, sizeof(K));
    memset(Jud, false, sizeof(Jud));
    memset(vis, 0, sizeof(vis));
    for(int i = 1; i <= M; i++){
        int a, b;
        cin >> a >> b;
        putin(a, b);
        putin(b, a);
    }
    return;
}

void BFS(int s)
{
    Jud[s] = true;
    memset(quens, 0, sizeof(quens));
    tails = 1;
    quens[1] = s;
    for(int i = 1; i <= tails; i++)
        for(int j = vis[quens[i]]; j; j = K[j].next)
            if(Jud[K[j].y] == false){
                quens[++tails] = K[j].y;
                Jud[K[j].y] = true;
            }
    return;
}

int main()
{
    ios::sync_with_stdio(false);
    Insert();
    for(int i = 1; i <= N; i++)
        if(Jud[i] == false){
            BFS(i);
            ans_groups++;
        }
    cout << ans_groups << endl;
    return 0;
}
    原文作者:犯罪团伙问题
    原文地址: https://blog.csdn.net/NathanielNIu/article/details/72328317
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞