hdu 1827 強連通縮點__include

來源:互聯網
上載者:User
#include <iostream>#include <cstring>#include <stack>#define INF 0x3f3f3f3f#define clr(x, k) memset((x),(k),sizeof(x))using namespace std;const int N = 1001;struct edge{int s, e, next;}edg1[2*N], edg2[2*N];int n, m, ct, index, et1, et2;int head[N], dfn[N], low[N], belong[N];int p[N], indegree[N], mmm[N];bool instack[N];stack <int> s;void add_e(int u, int v, edge edg[], int &et){edg[et].s = u;edg[et].e = v;edg[et].next = head[u];head[u] = et++;}void read(){int i, u, v;for (i=1;i<=n;++i){scanf("%d", &p[i]);}et1 = 0;clr(head, -1);while (m--){scanf("%d %d", &u, &v);add_e(u, v, edg1, et1);}}void tarjan(int i){int j;dfn[i] = low[i] = ++index;s.push(i);instack[i] = true;for (int u=head[i];u!=-1;u=edg1[u].next){j = edg1[u].e;if (dfn[j]==0){tarjan(j);if (low[i]>low[j]) low[i] = low[j];}elseif (instack[j] && low[i]>dfn[j])    low[i] = dfn[j];}if (low[i] == dfn[i]){ct++;do{j = s.top(), s.pop();instack[j] = false;belong[j] = ct;if (mmm[ct]>p[j]) mmm[ct] = p[j];}while (i!=j);}}void ace(){int i;clr(mmm, INF);ct = index = 0;clr(dfn, 0);clr(instack, 0);for (i=1;i<=n;++i){if (!dfn[i]) tarjan(i);}int x;et2 = 0;clr(head, -1);clr(indegree, 0);for (i=0;i<et1;++i){int j = edg1[i].s;int k = edg1[i].e;if (belong[j] != belong[k]){add_e(belong[j], belong[k], edg2, et2);indegree[belong[k]]++;}//  if (mmm[belong[j]]>p[j]) mmm[belong[j]] = p[j];//  if (mmm[belong[k]]>p[k]) mmm[belong[k]] = p[k];}int sum = 0, num = 0;for (i=1;i<=ct;i++){if (indegree[i]==0){sum += mmm[i];num++;}}printf("%d %d\n", num, sum);}int main(){while (~scanf("%d %d", &n, &m)){read();ace();}return 0;}

劃掉的兩個if:mmm數組要在tarjan裡更新,可能有孤點存在。。。。

for simplicity 借鑒了鳥神的ace~

 

 

 

聯繫我們

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