L2-013. 红色警报

连通子图的个数来判断有多少无法连通的区域,当失去一座城市的时候,如果连通子图的个数增加大于1,则整个国家的连通性被改变,增加数为0(单独的一个城市,本身不和其他任何城市相连)或1(边缘城市,和子图之间只有一条通路)说明没有改变连通性

#include <iostream>
#include <stdlib.h>
using namespace std;
int **e;
bool *city;
int n, m, k;


void dfs(int a)
{
    city[a] = true;
    for(int i=0; i<n; i++)
    {
        if(city[i]==false&&e[i][a]==1)
        {
            dfs(i);
        }

    }

}
int countcnt()
{
    int cnt = 0;
    fill(city, city+n, false);
    for(int i=0; i<n; i++)
    {
        if(city[i]==false)
        {
            dfs(i);
            cnt++;
        }
    }
    return cnt;
}
int main()
{

    cin >> n >> m;

    e = (int **)malloc(sizeof(int*)*(n+1));
    for(int i=0; i<n+1; i++)
        e[i] = (int*)malloc(sizeof(int)*(n+1));

    city = (bool *)malloc(sizeof(bool)*(n+1));

    for(int i=0; i<n+1; i++)
        for(int j=0; j>n+1; j++)
            e[i][j]=0;
    int temp1, temp2;


    for(int i=0; i<m; i++)
    {
        cin >> temp1 >> temp2;
        e[temp1][temp2] = e[temp2][temp1]= 1;
    }
    cin >> k;
    int cnt = countcnt();
    for(int i=0; i<k; i++)
    {
        cin >> temp1;
        for(int j=0; j<n; j++)
        {
         e[temp1][j] = 0;
         e[j][temp1] = 0;
        }

         int tempcnt = countcnt();

         if(tempcnt<=cnt+1)
                cout << "City " << temp1 << " is lost.\n";
         else
         {
            cout << "Red Alert: City " << temp1 <<  " is lost!\n";
         }
             if(i==n-1)
         {
             cout << "Game Over.\n";
         }
         cnt = tempcnt;


    }
    return 0;
}

 

转载于:https://www.cnblogs.com/1915884031A-qqcom/p/7454058.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值