POJ 1236 Network of Schools(強連通分量)

來源:互聯網
上載者:User

標籤:poj   圖論   圖的連通性   強連通分量   

POJ 1236 Network of Schools連結:http://poj.org/problem?id=1236
題意:有一些學校串連到一個電腦網路。這些學校之間達成了一個協議:每個學校維護著一個學校列表,它向學校列表中的學校發布軟體。注意,如果學校B 在學校A 的列表中,則A 不一定在B 的列表中。
任務A:計算為使得每個學校都能通過網路收到軟體,你至少需要準備多少份軟體拷貝。
任務B:考慮一個更長遠的任務,想確保給任意一個學校發放一個新的軟體拷貝,該軟體拷貝能發布到網路中的每個學校。為了達到這個目標,必須在列表中增加新成員。計算需要添加新成員的最小數目。
思路:給定一張有向圖,問A:最少需要選擇幾個點,才能訪問完所有的點縮點之後的DAG中,入度為0的點的個數即為答案,因為這些點不能通過其他點訪問到。
B:最少添加多少條邊,使得原圖成為一個強連通分量1. 先進行強連通縮點,形成一個有向非循環圖
2. 統計出度為0: outcnt 和入度為0: incnt
3. 如果已經為一個強連通分量,答案為0,否則答案為max(incnt, outcnt)

/*ID: [email protected]PROG:LANG: C++*/#include<map>#include<set>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<fstream>#include<cstring>#include<ctype.h>#include<iostream>#include<algorithm>using namespace std;#define INF (1<<30)#define PI acos(-1.0)#define mem(a, b) memset(a, b, sizeof(a))#define rep(i, a, n) for (int i = a; i < n; i++)#define per(i, a, n) for (int i = n - 1; i >= a; i--)#define eps 1e-6#define debug puts("===============")#define pb push_back#define mkp make_pair#define all(x) (x).begin(),(x).end()#define fi first#define se second#define SZ(x) ((int)(x).size())#define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m)typedef long long ll;typedef unsigned long long ULL;const int maxn = 110;vector<int> g[maxn];int n, m, dfn[maxn], low[maxn], scc_cnt, dfs_clock, sccno[maxn];stack<int> s;void dfs(int u) {    low[u] = dfn[u] = ++dfs_clock;    s.push(u);    for (int i = 0; i < g[u].size(); i++) {        int v = g[u][i];        if (!dfn[v]) {            dfs(v);            low[u] = min(low[v], low[u]);        } else if (!sccno[v]) low[u] = min(low[u], dfn[v]);    }    if (low[u] == dfn[u]) {        scc_cnt++;        while(1) {            int x = s.top();            s.pop();            sccno[x] = scc_cnt;            if (x == u) break;        }    }}void find_scc(int n) {    mem(dfn, 0);    mem(sccno, 0);    scc_cnt = dfs_clock = 0;    for (int i = 1; i <= n; i++) if (!dfn[i]) dfs(i);}int in[maxn], out[maxn];int incnt, outcnt;int work() {    for (int u = 1; u <= n; u++) {        for (int i = 0; i < g[u].size(); i++) {            int U = sccno[u], V = sccno[g[u][i]];            if (U != V) out[U]++, in[V]++;        }    }    incnt = outcnt = 0;    //cout<<scc_cnt<<endl;    for (int i = 1; i <= scc_cnt; i++) {        if (!in[i]) incnt++;        if (!out[i]) outcnt++;    }    printf("%d\n", incnt);    if (scc_cnt == 1) printf("0\n");    else printf("%d\n", max(incnt, outcnt));}int main () {    scanf("%d", &n);    for (int i = 1; i <= n; i++) {        int x;        while(scanf("%d", &x), x) {            g[i].pb(x);        }    }    find_scc(n);    work();    return 0;}


POJ 1236 Network of Schools(強連通分量)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.