【PAT甲级】1013. Battle Over Cities (25)

来源:互联网 发布:邮箱数据吧 编辑:程序博客网 时间:2024/06/05 18:47
#include <stdio.h>#include <string.h>int v[1001][1001] = {0};int w[1001] = {0};int n, m, k;void markMatrix(int a, int from, int count) {    int j, k;    for (k = 1; k <= n; k++) {        if (k == a) continue;        if (v[from][k] == 1 && w[k] == 0) {            w[k] = count;            markMatrix(a, k, count);        }    }}int main(int argc, char *argv[]) {    scanf("%d %d %d", &n, &m, &k);    int i, j, l, s;    for (i = 0; i < m; i++) {        int a, b;        scanf("%d %d", &a, &b);        v[a][b] = v[b][a] = 1;    }    for (i = 0; i < k; i++) {        int a;        scanf("%d", &a);        memset(w, 0, 1001 * sizeof(int));        int count = 0;        for (s = 1; s <= n; s++) {            if (s == a) continue;            if (w[s] == 0) {                 count++;                w[s] = count;                markMatrix(a, s, count);//这里的a是要排除的            }        }               printf("%d\n", count - 1);    }    return 0;}
0 0