1. 程式人生 > >PAT 1013 Battle Over Cities

PAT 1013 Battle Over Cities

from 利用 text pac for flex pan -s 輸入

1013 Battle Over Cities (25 分)

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 (<), 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<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxnum 100005
int n,m,k;
int mp[1005][1005]; int temp[1005][1005]; int vis[1005] = {0}; int findnotvis(){ for(int i=1;i <= n;i++){ if(!vis[i]) return i; } return -1; } void dfs(int x){ vis[x] = 1; for(int i=1;i <= n;i++){ if(!vis[i]&&mp[x][i]){ dfs(i); } } } int main(){ cin >> n >> m >> k; memset(mp,0, sizeof(mp)); for(int i=0;i < m;i++){ int x,y;scanf("%d%d",&x,&y); mp[x][y] = mp[y][x] = 1; } while(k--){ int num;cin >> num; vis[num] = 1; //只要把點置為訪問過的就可以了,不需要刪去邊 int cnt = 0; for(int i=1;i <= n;i++){ //這個寫法很好,學習了。 if(!vis[i]){ dfs(i); cnt++; } } cout << cnt-1 << endl; memset(vis,0, sizeof(vis)); } return 0; }

果然用cin輸入數據會超時。。

其實這是考察圖的問題,刪除圖的一個節點,是其他節點成為連通圖,至少需要添加多少條線

添加最少的路線,就是連通分量數-1(例:n個互相獨立的連通分量組成一個連通圖,只需要連n-1條線就可以)

這道題最重要就是求除去圖的一個節點後 剩余的連通分量的數目

利用鄰接矩陣v存儲路線,用visit數組表示城市是否被遍歷過

對於每個被占領的城市,將其表示為遍歷過的狀態true即可

利用深度優先遍歷dfs計算連通分量數目

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int N=1010;
int n,m,a[N*N],b[N*N],pre[N];
 
int f(int x){
    if(pre[x]==x)return x;
    pre[x]=f(pre[x]);
    return pre[x];
}
 
void link(int x,int y){
    int r1=f(x),r2=f(y);
    if(r1!=r2){
        pre[r2]=r1;
    }
}
void solve(int city){
    for(int i=1;i<=n;i++){
        pre[i]=i;
    }
    for(int i=0;i<m;i++){
        if(a[i]==city||b[i]==city)continue;
        link(a[i],b[i]);
    }
    int ans=0;
    for(int i=1;i<=n;i++){   //查找堆的個數
        if(i==city)continue;
        int ri=f(i);  
        if(ri==i)ans++; //是個堆頂
} printf("%d\n",ans-1); } int main(){ int k; scanf("%d%d%d",&n,&m,&k); for(int i=0;i<m;i++){ scanf("%d%d",&a[i],&b[i]); } int x; for(int i=1;i<=k;i++){ scanf("%d",&x); solve(x); } }

用並查集也挺好的思想

 

PAT 1013 Battle Over Cities