1013 Battle Over Cities (25)(25 分)

DFS遍历图即可,某个城市失去将vis置为true即可,注意每次都要vis重新初始化

#include
#include
using namespace std;
const int maxv = 1e3 + 10;
int G[maxv][maxv];
bool vis[maxv];
int n, m, k;
void DFS(int v)
{
    vis[v] = true;
    for (int i = 1; i <= n; i++)
    {
        if (!vis[i] && G[v][i])DFS(i);
    }
}
int main()
{
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < m; i++)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        G[x][y] = G[y][x] = 1;
    }
    while (k--)
    {
        int query;
        scanf("%d", &query);
        memset(vis, false, sizeof(vis));
        vis[query] = true;
        int ans = 0;
        for (int i = 1; i <= n; i++)
        {
            if (!vis[i])
            {
                DFS(i);
                ans++;
            }
        }
        printf("%d\n", ans - 1);
    }
    return 0;
}

你可能感兴趣的:(1013 Battle Over Cities (25)(25 分))