Codeforces 445B DZY Loves Chemistry (query set), dzylovesphysics

Source: Internet
Author: User

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;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.