PAT甲題題解-1013. Battle Over Cities (25)-求聯通分支個數

來源:互聯網
上載者:User

標籤:amp   分支   多少   set   範圍   span   turn   string   ack   

題目就是求聯通分支個數
刪除一個點,剩下聯通分支個數為cnt,那麼需要建立cnt-1邊才能把這cnt個聯通分支個數求出來
怎麼求聯通分支個數呢
可以用並查集,但並查集的話複雜度是O(m*logn*k)
我這裡用的是dfs,dfs的複雜度只要O((m+n)*k)
這裡k是指因為有k個點要查詢,每個都要求一下刪除後的聯通分支數。
題目沒給定m的範圍,所以如果m很大的話,dfs時間會比較小。

for一遍1~n個點,每次從一個未標記的點u開始dfs,標記該dfs中訪問過的點。
u未標記過,說明之前dfs的時候沒訪問過該點,即表明該點與前面的點不屬於同一個分支,相當於新的分支。
所以只要統計一下1~n中dfs多少次,就有多少個聯通分支
刪除的點最開始標記一下,就不會訪問到了。

#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <vector>#include <cstring>using namespace std;const int maxn=1000+5;int n,m,k;int check[maxn]; //先標記哪些點會check,防止有重複的點做重複的工作int ans[maxn]; //刪掉節點i後剩餘的連通分支數目int vis[maxn]; //用於dfs時候的標記struct Edge{    int to;    int next;}edge[maxn*maxn];int head[maxn];int tot;void init(){    memset(head,-1,sizeof(head));    tot=0;}void add(int u,int v){    edge[tot].to=v;    edge[tot].next=head[u];    head[u]=tot++;}void dfs(int u){    vis[u]=1;    if(head[u]==-1)        return;    for(int k=head[u];k!=-1;k=edge[k].next){        int v=edge[k].to;        if(!vis[v]){            dfs(v);        }    }}/*對於要check的點,求刪除後的聯通分支數,儲存在ans數組裡*/void solve(){    memset(ans,0,sizeof(ans));    for(int i=1;i<=n;i++){        //如果i是要被詢問的        if(check[i]){            memset(vis,0,sizeof(vis));            vis[i]=1;            for(int j=1;j<=n;j++){                //每次有沒被訪問過的節點,表明又是一個新的聯通分支                if(!vis[j] && j!=i){                    dfs(j);                    ans[i]++;                }            }        }    }}int main(){    int u,v;    init();    scanf("%d %d %d",&n,&m,&k);    for(int i=0;i<m;i++){        scanf("%d %d",&u,&v);        add(u,v);        add(v,u);    }    memset(check,0,sizeof(check));    vector<int>query;    for(int i=0;i<k;i++){        scanf("%d",&u);        check[u]=1;        query.push_back(u);    }    solve();    for(int i=0;i<query.size();i++){        u=query[i];        printf("%d\n",ans[u]-1);    }    return 0;}
View Code

 

PAT甲題題解-1013. Battle Over Cities (25)-求聯通分支個數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.