1. 程式人生 > >1013 Battle Over Cities(PAT 甲等 C++實現)

1013 Battle Over Cities(PAT 甲等 C++實現)

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

鄰接矩陣程式碼:

#include <iostream>    
#include <algorithm>
using namespace std;
   
void DFS(int v[][1005],bool visit[],int n,int node){
    visit[node]=true;
    for(int i=1;i<=n;++i){
        if(visit[i]==false && v[node][i]==1){
            DFS(v,visit,n,i);
        }
    } 
}

// 1103 Integer Factorization (30 point(s))
int main(void){    
    int n,m,k;   // n=cities m=remaining highways k=checked
    cin>>n>>m>>k;// 頂點 邊 查詢數
     
    int v[1005][1005]={0};     // 鄰接矩陣 
    int temp1,temp2;
    for(int i=1;i<=m;++i){    // 邊連通 
        cin>>temp1>>temp2; 
        v[temp1][temp2]=1;    // 無向邊
        v[temp2][temp1]=1;
    }
      
    for(int i=1;i<=k;++i){    // 查詢次數
        bool visit[1005]={false}; // 標記 
        //fill(visit, visit+1005, false); // 賦值
        int temp,sum=0; 
        cin>>temp;
        visit[temp]=true; 
          
        for(int j=1;j<=n;++j){
            if(visit[j]==false){
                DFS(v,visit,n,j);
                ++sum;
            }
        }
        cout<<(sum-1)<<endl;
    }

    return 0;
} // jinzheng 2018.9.19 17:22

鄰接表程式碼:

#include <iostream>     
#include <vector>
using namespace std;
   
vector<int> G[1005];
int current; 

// 深度優先遍歷
void dfs(bool visit[],int v){
    if(v==current) return;
    visit[v] = true;

    for(int i=0;i<G[v].size();++i){
        if(visit[G[v][i]]==false){
            dfs(visit,G[v][i]);
        }
    }
}  

// 1013 Battle Over Cities (25 point(s))
int main(void){     
    int n,m,k;
    cin>>n>>m>>k; 
    
    for(int i=1;i<=m;++i){
        int temp1,temp2;
        cin>>temp1>>temp2;
        G[temp1].push_back(temp2);
        G[temp2].push_back(temp1);
    }

    for(int i=1;i<=k;++i){
        cin>>current;
        bool visit[1005]={false};

        int sum=0;
        for(int j=1;j<=n;++j){
            if(j!=current && visit[j]==false){
                dfs(visit,j);
                ++sum;
            }
        } 
        cout<<(sum-1)<<endl; 
    }

    return 0;
} // jinzheng 2018.9.19 21:09