hdu--4912--終於解脫了

來源:互聯網
上載者:User

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

....曆經n發wa之後 終於解脫了

這感覺....無法言表...

其實 這題你說難吧? 對於我而言 一共就做過一道lca的人來說 是有點難的

可是吧 當你慢慢地寫下來 其實代碼那麼長 又不怎麼難

對於此題 就是先求出query中2個結點的lca之後 然後dfs它們的lca 根據深度進行排序

這個其實不難理解吧? 當然深度越大的 就是離根結點越遠 首先進行搜尋

然後 就是每搜到一個點 就將它進行標記-已訪問過

深搜的時候 注意下 不僅要判斷是否訪問過 而且要判斷這2點的深度關係 肯定是從小的搜到大的

最最注意的就是

depth[v] = depth[u] + 1這句話 不要太隨意地寫在了lca_tarjan(v)的後面

表示 我就一不留神 寫錯了 找了好久。。

其餘的 就是可以再去好好思索下lca_tarjan的整個演算法實現過程

準備 可能 晚上再去學校lca的線上演算法...

  1 #include <iostream>  2 #include <cstring>  3 #include <algorithm>  4 using namespace std;  5   6 int Ncnt , Qcnt;  7 const int size = 100010;  8 struct data  9 { 10     int id; 11     int from; 12     int to; 13     int next; 14     int lca; 15 }; 16 data node[size*2]; 17 int Nhead[size]; 18 data query[size*2]; 19 data graph[size]; 20 int Qhead[size]; 21 bool vis[size]; 22 int father[size]; 23 int depth[size]; 24 bool check[size]; 25  26 bool cmp( data p , data q ) 27 { 28     return depth[p.lca] > depth[q.lca]; 29 } 30  31 void init( ) 32 { 33     memset( Nhead , -1 , sizeof(Nhead) ); 34     memset( Qhead , -1 , sizeof(Qhead) ); 35     memset( vis , false , sizeof(vis) ); 36     memset( check , false , sizeof(check) ); 37     depth[1] = 0; 38 } 39  40 int find( int x ) 41 { 42     return x == father[x] ? x : father[x] = find( father[x] ); 43 } 44  45 void addNedge( int st , int end ) 46 { 47     node[Ncnt].to = end; 48     node[Ncnt].next = Nhead[st]; 49     Nhead[st] = Ncnt ++; 50 } 51  52 void addQedge( int id , int st , int end ) 53 { 54     query[Qcnt].id = id; 55     query[Qcnt].from = st; 56     query[Qcnt].to = end; 57     query[Qcnt].next = Qhead[st]; 58     Qhead[st] = Qcnt ++; 59 } 60  61 void lca_tarjan( int u ) 62 { 63     int v; 64     father[u] = u; 65     vis[u] = true; 66     for( int i = Nhead[u] ; ~i ; i = node[i].next ) 67     { 68         v = node[i].to; 69         if( !vis[v] ) 70         { 71             depth[v] = depth[u] + 1; 72             lca_tarjan( v ); 73             father[v] = u; 74         } 75     } 76     for( int i = Qhead[u] ; ~i ; i = query[i].next ) 77     { 78         v = query[i].to; 79         if( vis[v] ) 80         { 81             graph[ query[i].id ].lca = find(v); 82         } 83     } 84 }     85  86 void dfs( int u ) 87 { 88     int v; 89     check[u] = true; 90     for( int i = Nhead[u] ; ~i ; i = node[i].next ) 91     { 92         v = node[i].to; 93         if( !check[v] && depth[u]<depth[v] ) 94             dfs(v); 95     } 96 } 97  98 int main() 99 {100     cin.sync_with_stdio(false);101     int n , m , st , end , lca , ans , temp;102     while( cin >> n >> m )103     {104         ans = Ncnt = Qcnt = 0;105         init( );106         for( int i = 0 ; i<n-1 ; i++ )107         {108             cin >> st >> end;109             addNedge( st , end );110             addNedge( end , st );111         }112         for( int i = 0 ; i<m ; i++ )113         {114             cin >> st >> end;115             addQedge( i , st , end );116             addQedge( i , end , st );117             graph[i].from = st;118             graph[i].to = end;119         }120         lca_tarjan(1);121         sort( graph , graph+m , cmp );122         for( int i = 0 ; i<m ; i++ )123         {124             st = graph[i].from;125             end = graph[i].to;126             lca = graph[i].lca;127             if( !check[st] && !check[end] )128             {129                 ans ++;130                 dfs( lca );131             }132         }133         cout << ans << endl;134     }135     return 0;136 }
View Code

 

today:

  關于思念你的這件小事

  躲得過對酒當歌的夜晚

  躲不過四下無人的街道

 

hdu--4912--終於解脫了

聯繫我們

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