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 174 ——D.Alter Altar(思维)
AtCoder Beginner Contest 174 ——D.Alter Altar(思维)
61 0
1061. Dating (20)
Sherlock Holmes received a note with some strange strings: "Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm".
810 0
|
机器学习/深度学习 并行计算 Java
A Knight&#39;s Journey
总时间限制: 1000ms 内存限制: 65536kB描述BackgroundThe knight is getting bored of seeing the same black and white squares again and again and has decided to make a journeyaround the world.
1146 0
[Everyday Mathematics]20150306
在王高雄等《常微分方程(第三版)》习题 2.5 第 1 题第 (32) 小题: $$\bex \frac{\rd y}{\rd x}+\frac{1+xy^3}{1+x^3y}=0. \eex$$   解答: $$\beex \bea 0&=(1+xy^3)\rd x+(1+x^3y)\rd y...
641 0
[Everyday Mathematics]20150305
设 $f\in C^2[0,1]$, $$\bex f(0)=-1,\quad f'(1)=3,\quad \int_0^1 xf''(x)\rd x=1. \eex$$ 试求 $f(1)$.   解答: $$\beex \bea 1&=\int_0^1 x\rd f'(x)\\ &=xf'(x...
672 0
[Everyday Mathematics]20150223
是否存在 $3\times 3$ 阶实方阵 $A$ 使得 $\tr A=0$ 且 $A^2+A^T=I$?
508 0
[Everyday Mathematics]20150222
设 $$\bex a_0=1,\quad a_1=\frac{1}{2},\quad a_{n+1}=\frac{na_n^2}{1+(n+1)a_n}\ (n\geq 1). \eex$$ 试证: $\dps{\sum_{k=0}^\infty\frac{a_{k+1}}{a_k}}$ 收敛, 并求其值.
671 0
[Everyday Mathematics]20150214
设 $\dps{x\in \sex{0,\frac{\pi}{2}}}$, 试比较 $\tan(\sin x)$ 和 $\sin(\tan x)$.
551 0