连通分量

Connected Components

Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.

Input

In the first line, two integer nn and mm are given. nn is the number of users in the SNS and mm is the number of relations in the SNS. The users in the SNS are identified by IDs 0,1,...,n10,1,...,n−1.

In the following mm lines, the relations are given. Each relation is given by two integers ss and tt that represents ss and tt are friends (and reachable each other).

In the next line, the number of queries qq is given. In the following qq lines, qq queries are given respectively. Each query consists of two integers ss and tt separated by a space character.

Output

For each query, print "yes" if tt is reachable from ss through the social network, "no" otherwise.

Constraints

  • 2n100,0002≤n≤100,000
  • 0m100,0000≤m≤100,000
  • 1q10,0001≤q≤10,000

Sample Input

10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3

Sample Output

yes
yes
no
题意:
无向图求连通分量。
思路:
dfs或者bfs搜索即可,基础。
代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<string>
using namespace std;
typedef long long ll;
const int N_MAX = 100000+2;
vector<int>G[N_MAX];
int n,m;
int Q;
int color[N_MAX];

void dfs(int v,int c) {
    color[v] = c;
    int size = G[v].size();
    for (int i = 0; i < size;i++) {
        int to = G[v][i];
        if(!color[to])
            dfs(to, c);
    }
}

int main() {
    while (scanf("%d%d",&n,&m)!=EOF) {
        memset(color, 0, sizeof(color));
        for (int i = 0; i < n;i++) {
            G[i].clear();
        }
        for (int i = 0; i < m;i++) {
            int from, to;
            scanf("%d%d",&from,&to);
            G[from].push_back(to);
            G[to].push_back(from);
        }
        int c = 1;
        for (int i = 0; i < n;i++) {
            if (!color[i]) {
                dfs(i, c);
                c++;
            }
        }
        
        scanf("%d",&Q);
        for (int i = 0; i < Q;i++) {
            int from, to;
            scanf("%d%d",&from,&to);
            if (color[from] == color[to])
                puts("yes");
            else puts("no");
        }
    } 

    return 0;
}

或者并查集也可解决

代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<string>
using namespace std;
typedef long long ll;
const int N_MAX = 100000+2;

int par[N_MAX];
int Rank[N_MAX];
void init(int n) {
    for (int i = 0; i < n;i++) {
        par[i] = i;
        Rank[i] = 0;
    }
}

int find(int x) {
    if (par[x] == x)
        return x;
    else {
        return par[x] = find(par[x]);
    }
}

void unite(int x,int y) {
    x = find(x);
    y = find(y);
    if (x == y)return;
    if (Rank[x] < Rank[y]) {
        par[x] = y;
    }
    else {
        par[y] = x;
        if (Rank[x] == Rank[y])Rank[x]++;
    }
}
bool same(int x,int y) {
    return find(x) == find(y);
}

int n,m;
int Q;
int main() {
    while (scanf("%d%d",&n,&m)!=EOF) {
        init(n);
        for (int i = 0; i < m;i++) {
            int from, to;
            scanf("%d%d",&from,&to);
            unite(from, to);
        }
        scanf("%d",&Q);
        for (int i = 0; i < Q;i++) {
            int from, to;
            scanf("%d%d",&from,&to);
            if (same(from,to))
                puts("yes");
            else puts("no");
        }
    } 

    return 0;
}



原文地址:https://www.cnblogs.com/ZefengYao/p/7732598.html