hdu--3836--tarjan+縮點

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   2014   

縮點 很簡單的啊... 就是將原來一個連通塊變成一個點..

可能你原本是這樣的  A->B->C->A  縮點完成後 我們就把{A,B,C}用數字1來表示 如果還有D->E->D 那我們再講{D,E}用2表示....

最後的sum就是代表連通塊總的個數

然後 一般 縮點完成後 我們現在得到了n個連通塊 我們要考慮的是 最少需要添加幾條邊來把它串聯起來

這樣考慮

假如給你2個點   A B 想要讓他們聯通 需要在他們之間建立兩條邊 分別是A->B   B->A 你會發現這樣 A B的入度 出度分別從0變成了1

如果是這樣的情況 那麼 現在A的出度為2 入度為0   B和C的出度為0 入度為1 我們只需要B->A  C->A建立邊就可以了

這樣我們就能達到B C的出度增加 A的入度增加

其實 是因為 在任何一個連通圖中每個點的入度和出度都是>=1這樣才能達到讓別人聯向自己 自己通向別人

所以 我們只需要求出在縮點完成後的圖中   入度為0 與 出度為0的點的個數大小就可以了  取大的

******************

如果是做題的話 有一點要小心 很容易WA在這裡..不能直接判斷max(in,out)來輸出 而是首先判斷縮點後的圖 是不是連通圖 是的話就直接輸出0了 如果直接輸出max(in,out)就是1了.......

  1 #include <iostream>  2 #include <cstring>  3 #include <algorithm>  4 #include <stack>  5 using namespace std;  6   7 int n , cnt , index , sum;  8 const int size = 20010;  9 struct data 10 { 11     int st; 12     int end; 13 }node[size*3]; 14 struct graph 15 { 16     int to; 17     int next; 18 }edge[size*3]; 19 int head[size]; 20 int father[size]; 21 int No[size]; 22 int scc[size]; 23 int in[size]; 24 int out[size]; 25 bool vis[size]; 26 stack<int>s; 27  28 void init( ) 29 { 30     memset( head , -1 , int*sizeof(n+10) ); 31     memset( No , 0 , int*sizeof(n+10) ); 32     memset( father , 0 , int*sizeof(n+10) ); 33     memset( vis , false , int*sizeof(n+10) ); 34     memset( scc , 0 , int*sizeof(n+10) ); 35     memset( in , 0 , int*sizeof(n+10) ); 36     memset( out , 0 , int*sizeof(n+10) ); 37 }     38  39 void add( int st , int end ) 40 { 41     edge[cnt].to = end; 42     edge[cnt].next = head[st]; 43     head[st] = cnt ++; 44 } 45  46 void tarjan( int u ) 47 { 48     int v , temp; 49     No[u] = father[u] = index++; 50     vis[u] = true; 51     s.push(u); 52     for( int i = head[u] ; ~i ; i = edge[i].next ) 53     { 54         v = edge[i].to; 55         if( !No[v] ) 56         { 57             tarjan(v); 58             father[u] = min( father[u] , father[v] ); 59         } 60         else if( vis[v] ) 61         { 62             father[u] = min( father[u] , No[v] ); 63         } 64     } 65     if( father[u] == No[u] ) 66     { 67         sum ++; 68         while(1) 69         { 70             temp = s.top(); 71             s.pop(); 72             vis[temp] = false; 73             scc[temp] = sum; 74             if( father[temp] == No[temp] ) 75                 break; 76         } 77     } 78 } 79  80 int main() 81 { 82     cin.sync_with_stdio(false); 83     int m , st , end , inMax , outMax; 84     while( cin >> n >> m ) 85     { 86         inMax = outMax = cnt = sum = 0; 87         index = 1; 88         init(); 89         for( int i = 0 ; i<m ; i++ ) 90         { 91             cin >> st >> end; 92             add( st , end ); 93             node[i].st = st; 94             node[i].end = end; 95         } 96         for( int i = 1 ; i<=n ; i++ ) 97         { 98             if( !No[i] ) 99                 tarjan(i);100         }101         for( int i = 0 ; i<m ; i++ )102         {103             if( scc[ node[i].st ]!=scc[ node[i].end ] )104             {105                 out[ scc[node[i].st] ] ++;106                 in[ scc[node[i].end] ] ++;107             }108         }109         for( int i = 1 ; i<=sum ; i++ )110         {111             if( !in[i] )112                 inMax ++;113             if( !out[i] )114                 outMax ++;115         }116         if( sum==1 )117             cout << "0" << endl;118         else119             cout << max( inMax , outMax ) << endl;120     }121     return 0;122 }
View Code


因為 這裡的數組要開的TM的實在是太多了 所以我用了 (n)*sizeof(int)來初始化 我測了下 節約了15ms....

 

today:

  後來我終於知道

  它並不是我的花

  我只是恰好途徑了它的盛放

 

hdu--3836--tarjan+縮點

聯繫我們

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