TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 強連通分量

來源:互聯網
上載者:User
It's not Floyd Algorithm 時間限制(普通/Java):1000MS/3000MS     運行記憶體限制:65536KByte
 

描述

When a directed graph is given, we can solve its transitive closure easily using the well-known Floyd algorithm.

But if you're given a transitive closure, can you give a corresponding directed graph with minimal edges?

輸入

About 100 test cases, seperated by blank line.

First line of each case is an integer N (1<=N<=200). The followingN lines represent the given transitive closure in 0-1 matrix form, each line hasN numbers.

輸出

For each case, just output the number of minimal edges of a directed graph which has a given transitive closure.

範例輸入

1121 00 121 11 131 1 10 1 10 0 1

範例輸出

0022

提示

Transitive closure can be presented as a matrix T, where Ti,j is true if and only if there is a path from vertexi toj.

首先縮點 在建圖 對於每個強連通分量如果有n個點那麼最少只需n條邊就可以聯通(n = 1 除外)

然後對於縮點後的圖咋做一次反閉包 去掉多餘的邊 在統計一下

#include<stdio.h>#include<string.h>int n;int dfn[210];int low[210];bool instack[210];int stack[210];int cnt,num,top;int a[210][210];int count[210];int belong[210];int map[210][210];void floyd(){int i,j,k;for(k = 1;k <= cnt; k++)for(i = 1;i <= cnt; i++)for(j = 1;j <= cnt; j++)if(map[i][k] && map[k][j] && map[i][j])map[i][j] = 0;}void tarjan(int i){        int j,k;        dfn[i] = low[i] = ++num;        instack[i] = true;        stack[++top] = i;        for(j = 1;j <= n; j++)        {            k = a[i][j];            if(!k)            continue;            if(!dfn[j])            {                tarjan(j);                if(low[i] > low[j])                low[i] = low[j];            }            else if(instack[j] && low[i] > dfn[j])                low[i] = dfn[j];        }         if(low[i] == dfn[i])        {            cnt++;            do            {                j = stack[top--];                instack[j] = false;                belong[j] = cnt;                 count[cnt]++;            }            while(i != j);        }} int main(){int i,j,sum;while(scanf("%d",&n)!=EOF){for(i = 1;i <= n; i++)for(j = 1; j<= n; j++)scanf("%d",&a[i][j]);num = top = cnt = 0;memset(dfn,0,sizeof(dfn));memset(instack,false,sizeof(instack));memset(count,0,sizeof(count));memset(map,0,sizeof(map));for(i = 1;i <= n; i++)if(!dfn[i])tarjan(i);for(i = 1; i<= n; i++)for(j = 1; j<= n; j++)if(a[i][j])if(belong[i] != belong[j])map[belong[i]][belong[j]] = 1;sum = 0;floyd();for(i = 1;i <= cnt; i++)if(count[i] != 1)sum += count[i];for(i = 1;i <= cnt; i++)for(j = 1; j<= cnt; j++)if(map[i][j])sum++;printf("%d\n",sum);}return 0;} 


 

 

聯繫我們

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