Codeforces 445B DZY Loves Chemistry (query set), dzylovesphysics
Link: Codeforces 445B DZY Loves Chemistry
There are several kinds of chemicals that give the relationship between the two sessions. Now we need to put the drugs in one container in sequence, and the chemicals in the container can interact with each other, if the current drug can respond to a drug already in the container, the risk value doubles, that is, * 2, the initial value is 1, and the order is obtained to maximize the risk value.
Solution: Query and find the smallest connected component s, and 2n-s is the answer.
#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;}