1013 Battle Over Cities (25 point(s))

1013 Battle Over Cities (25 point(s))

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output Specification:

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

Sample Input:

3 2 3
1 2
1 3
1 2 3

Sample Output:

1
0
0

题目大意:

给定对应的定点数量 N   (1..N)

给定对应的路径(互相连通)

给定去掉对应定点的序列

求出,每次去掉某一个定点后,要使得这个图依然连通的所要添加的边数的数量

思路:

广度优先遍历,如果有X个独立的不连通的图的话,就要遍历X次

对应的,所要添加的边也就是对应的X-1条边

深度优先遍历也是同样

bfs

#include<iostream>
#include<queue>
using namespace std;

const int INF=0x3f3f3f3f;
const int MAXN = 1024;
int map[MAXN][MAXN];
bool vis[MAXN];
int N,M,K;

int find_NO_visID(){

    for(int i=1;i<=N;i++){
        if(!vis[i]){
            return i;
        }
    }
    return -1;
}


int  main(){


    fill(&map[0][0],&map[0][0]+MAXN*MAXN,INF);
    fill(vis,vis+MAXN,false);
    scanf("%d %d %d",&N,&M,&K);
    //cin>>N>>M>>K;
    queue<int> que;

    for(int i=0;i<M;i++){
        int a,b;
//        cin>>a>>b;
        scanf("%d %d",&a,&b);
        map[a][b]=1;
        map[b][a]=1;
    }

    for(int i=0;i<K;i++){
        int city;
        //cin>>city;
        scanf("%d",&city);
        vis[city] = true;

        int start=find_NO_visID();
        int count=0;
        while(start!=-1){
            count++;

            que.push(start);
            vis[start] = true;
            while(!que.empty()){
                int  id = que.front();
                que.pop();

                for(int i=1;i<=N;i++){
                    if(map[id][i]!=INF&&!vis[i]){
                        que.push(i);
                        vis[i]=true;
                    }
                }
            }
            start=find_NO_visID();
        }

        printf("%d\n",count-1);
        //cout<<count-1<<endl;
        fill(vis,vis+MAXN,false);
    }



    return 0;
}

dfs

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;

int N,M,K;
const int MAXN = 1024;
vector<int> map[MAXN];
bool vis[MAXN];

int find_NO_visID(){

    for(int i=1;i<=N;i++){
        if(!vis[i]){
            return i;
        }
    }
    return -1;
}

void dfs(int start){

    vis[start] = true;
    int length = map[start].size();
    for(int i=0;i<length;i++){
        if(!vis[map[start][i]]){
            vis[map[start][i]] = true;
            dfs(map[start][i]);
        }
    }
}

int main(){
    //cin>>N>>M>>K;
    scanf("%d %d %d",&N,&M,&K);
    for(int i=0;i<M;i++){
        int a,b;
        //cin>>a>>b;
        scanf("%d %d",&a,&b);

        map[a].push_back(b);
        map[b].push_back(a);
    }

    for(int i=0;i<K;i++){
        int city;
        fill(vis,vis+MAXN,0);

        scanf("%d",&city);
        //cin>>city;
        vis[city] = true;

        //int start=find_NO_visID();
        int count=0;
        for(int i=1;i<=N;i++){
            if(!vis[i]){
                vis[i] = true;
                count++;
                dfs(i);
            }
        }
        //cout<<count-1<<endl;
        printf("%d\n",count-1);

    }
    return 0;
}

注意点:

对应的输入输出不要用cin和cout  会最后超时

猜你喜欢

转载自blog.csdn.net/Willen_/article/details/84502644