PAT甲1013. Battle Over Cities (25)

#include <cstdio>
#include <cstdlib>
#include <map>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

const int maxn=1010;
int N,M,K,checkc;
int G[maxn][maxn]={0};
bool vis[maxn]={false};

void DFS(int nowC)
{
    if (nowC==checkc)return;
    vis[nowC]=true;
    for(int i=1;i<=N;i++)
    {
        if(G[nowC][i]>0)
        {
            if(vis[i]==false)
            {
                DFS(i);
            }
        }
    }
}

int DFST()
{
    int x=0;
    for(int i=1;i<=N;i++)
    {
        if(vis[i]==false&&i!=checkc)
        {
            x++;
            DFS(i);
        }
    }
    return x;
}

int main()
{
    scanf("%d%d%d",&N,&M,&K);
    for(int i=0;i<M;i++)
    {
        int c1,c2;
        scanf("%d%d",&c1,&c2);
        G[c1][c2]=1;
        G[c2][c1]=1;
    }
    for(int i=0;i<K;i++)
    {
        for(int j=0;j<maxn;j++)
        {
            vis[j]=false;
        }
        scanf("%d",&checkc);
        int count=DFST();
            printf("%d\n",count-1);
    }
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yhy489275918/article/details/80219092