1013. Battle Over Cities (25)

简介: //连通分量数量 - 1//如何求连通分量:#include #include #include #include using namespace std;int e[1000][1000], n;bool ...
//连通分量数量 - 1
//如何求连通分量:
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int e[1000][1000], n;
bool visited[1000];

void dfs(int index){
    visited[index] = true;
    for (int i = 1; i <= n; i++) {
        if(!visited[i] && e[index][i] == 1)
            dfs(i);
    }
}

int main(){
    int  m, k;
    fill(e[0], e[999] + 1000, 0);
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        e[a][b] = e[b][a] = 1;
    }

    for (int i = 0; i < k; i++) {
        int t;
        fill(visited, visited + 1000, false);//每个点都相当于一个新问题 所以需要重置
        scanf("%d", &t);
        int cnt = 0;
        visited[t] = true;//标记为已失去
        for (int j = 1; j <= n; j++) {//城市编号1 - n-1
            if(!visited[j]){//如果该点没有被访问过 进行一次深度优先搜索 此处存在几个未被访问的点 存在几个连通分量
                dfs(j);
                cnt++;
            }
        }
        printf("%d\n", cnt - 1);
    }

    return 0;
}


目录
相关文章
|
机器学习/深度学习
AtCoder Beginner Contest 218 C - Shapes (模拟)
AtCoder Beginner Contest 218 C - Shapes (模拟)
146 0
AtCoder Beginner Contest 174 ——D.Alter Altar(思维)
AtCoder Beginner Contest 174 ——D.Alter Altar(思维)
88 0
|
定位技术
German collegiate programming contest 2012 - Ski Jumping
首先用动能定理算出平抛的初速度v0,然后分三种情况,0~L/2,L/2~L,L~无穷远。
136 0
German collegiate programming contest 2012 - Ski Jumping
PKU 3624 Charm Bracelet
本文主要讲背包入门题