标签:style http color width os for
题目链接:Codeforces 445B DZY Loves Chemistry
题目大意:有若干种化学药品,给出两两会反应的关系,现在要将药物依次放入一个容器中,容器中的化学药品可以互相反应,如果当前放入的药品能与已经在容器中的某一药品反应,那么危险值翻倍,即*2,初始值为1,求一顺序,使得为危险值最大。
解题思路:并查集求最小联通分量s,2n?s即为答案。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50;
typedef long long ll;
int n, m, f[maxn+5];
int getfar(int x) {
return x == f[x] ? x : f[x] = getfar(f[x]);
}
ll power (ll x, int t) {
ll ans = 1;
while (t) {
if (t&1)
ans = ans * x;
x = x * x;
t /= 2;
}
return ans;
}
int main () {
scanf("%d%d", &n, &m);
for (int i = 0; i <= n; i++)
f[i] = i;
int c = n, a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
int fa = getfar(a);
int fb = getfar(b);
if (fa != fb) {
f[fa] = fb;
c--;
}
}
printf("%lld\n", power(2LL, n-c));
return 0;
}
Codeforces 445B DZY Loves Chemistry(并查集),布布扣,bubuko.com
Codeforces 445B DZY Loves Chemistry(并查集)
标签:style http color width os for
原文地址:http://blog.csdn.net/keshuai19940722/article/details/37544619