Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (並查集)

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   for   

題目連結

昨天晚上沒有做出來,剛看題目的時候還把題意理解錯了,當時想著以什麼樣的順序倒,想著就饒進去了,

也被題目下面的樣本分析給誤導了。

題意:

有1-n種化學藥劑  總共有m對試劑能反應,按不同的次序將1-n種試劑滴入試管,如果正在滴入的試劑能與已經滴入

的試劑反應,那麼危險數*2,否則維持不變。問最後最大的危險係數是多少。

分析:其實這個題根本不用考慮倒入的順序,只是分塊就行,結果就是每個子集裡元素的個數-1 和  的2的冪。

 1 #include <iostream> 2 #include <cstring> 3 #include <queue> 4 #include <cmath> 5 #include <cstdio> 6 #include <algorithm> 7 #define LL long long 8 using namespace std; 9 const int maxn = 50 + 10;10 int n, m, bin[maxn], f[maxn], cou;11 int find(int x)12 {13     return bin[x]==x?x:(bin[x]=find(bin[x]));14 }15 void merge(int x, int y)16 {17     int fx = find(x);18     int fy = find(y);19     if(fx != fy)20     {21         bin[fx] = fy;22         cou ++;23     }24 }25 int main()26 {27     int i, x, y;28     LL ans;29     while(~scanf("%d%d", &n, &m))30     {31         cou = 0;32         for(i = 1; i <= n; i++)33         bin[i] = i;34         while(m--)35         {36             cin>>x>>y;37             merge(x, y);38         }39         ans = pow(2, cou);40         cout<<ans<<endl;41     }42     return 0;43 }

 

聯繫我們

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