L2-013. Red Alert

#include<bits/stdc++.h>
using namespace std;
bool bok [510], des [510];
vector<int>g[510];
void dfs(int s){
    for(int i=0;i<g[s].size();i++)
    if(!bok[g[s][i]]&&!des[g[s][i]]){
        bok [g [s] [i]] = 1;
        dfs(g[s][i]);
    }
}
int main(){
    int n,m;
    cin>>n>>m;
    while(m--){
        int s,e;
        cin>>s>>e;
        g[s].push_back(e);
        g[e].push_back(s);
    }
    int cnt=0;
    for(int i=0;i<n;i++)
        if (! bok [i]) {
            dfs(i);
            side [i] = 1;
            cnt++;
        }
    int q;
    cin>>q;
    int city =n;
    while(q--){
        int t;
        cin>>t;
        des[t]=1;
        city--;
        int c=0;
        memset (book, 0, sizeof (book));
        for(int i=0;i<n;i++){
            if(!bok[i]&&!des[i]){
                c++;
                side [i] = 1;
                dfs(i);
            }
        }
        if(c>cnt)cout<<"Red Alert: City "<<t<<" is lost!"<<endl;
        else cout<<"City "<<t<<" is lost."<<endl;
        cnt = c;
    }
    if(city==0)cout<<"Game Over."<<endl;
    return 0;
}


The problem with this question is how to judge the change in connectivity and how to measure it

We can record the number of Unicom blocks before the deletion point and then search for the number of different Unicom blocks after the deletion point (excluding destroyed cities)

If this number is smaller then there may be fewer destroyed cities that would otherwise be isolated

And if the number increases, it must be a change in connectivity, so the red alert

Each time it is compared with the number of connected blocks that destroyed the city last time, so once a city is destroyed and the number of connected blocks increases, it must mean that the connectivity here has changed.

Violence... um...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732733&siteId=291194637