標籤: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 }