PAT 1013 Battle Over Cities (25 分)

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 city1-city2 and city1-city3. Then if city1is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

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

思路

  1. 可以使用并查集解決該問題
  2. 也可以把該問題看作是計算連通分支的個數(shù)

AC代碼,使用的是第二種思路

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1010;
bool G[maxn][maxn] = {false};
bool vis[maxn] = {false};
int n, m, k;

void DFS(int x, int war) {
    vis[x] = true;
    for (int i = 1; i <= n; ++i)
    {
        if(!vis[i] && G[x][i] && i != war) {
            DFS(i, war);
        }
    }
}

int main() {

    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < m; ++i)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        G[x][y] = true;
        G[y][x] = true;
    }

    for (int i = 0; i < k; ++i)
    {
        int war;
        scanf("%d", &war);
        int cnt = 0;
        fill(vis, vis+maxn, false);
        for (int i = 1; i <= n; ++i)
        {
            if(!vis[i] && i != war) {
                DFS(i, war);
                cnt++;
            }
        }
        cnt = cnt-1>0? cnt-1: 0;
        printf("%d\n", cnt);
        
    }
}

AC代碼依鸥,使用的是第一種思路

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子画畅,更是在濱河造成了極大的恐慌蝙泼,老刑警劉巖谣辞,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件迫摔,死亡現(xiàn)場離奇詭異,居然都是意外死亡泥从,警方通過查閱死者的電腦和手機句占,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來躯嫉,“玉大人纱烘,你說我怎么就攤上這事杨拐。” “怎么了擂啥?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵哄陶,是天一觀的道長。 經(jīng)常有香客問我哺壶,道長屋吨,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任变骡,我火速辦了婚禮,結(jié)果婚禮上芭逝,老公的妹妹穿的比我還像新娘塌碌。我一直安慰自己,他們只是感情好旬盯,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布台妆。 她就那樣靜靜地躺著,像睡著了一般胖翰。 火紅的嫁衣襯著肌膚如雪接剩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天萨咳,我揣著相機與錄音懊缺,去河邊找鬼。 笑死培他,一個胖子當著我的面吹牛鹃两,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播舀凛,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼俊扳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了猛遍?” 一聲冷哼從身側(cè)響起馋记,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎懊烤,沒想到半個月后梯醒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡腌紧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年冤馏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片寄啼。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡逮光,死狀恐怖代箭,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情涕刚,我是刑警寧澤嗡综,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站杜漠,受9級特大地震影響极景,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜驾茴,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一盼樟、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧锈至,春花似錦晨缴、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至们拙,卻和暖如春稍途,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背砚婆。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工械拍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人装盯。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓殊者,卻偏偏與公主長得像,于是被迫代替她去往敵國和親验夯。 傳聞我的和親對象是個殘疾皇子猖吴,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355