Codeforces Round #254 (Div. 2) SDUT2015暑假集训14级周赛2 (并查集)

DZY loves chemistry, and he enjoys mixing chemicals.

DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.

Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.

Find the maximum possible danger after pouring all the chemicals one by one in optimal order.

Input

The first line contains two space-separated integers n and m.

Each of the next m lines contains two space-separated integers xi and yi(1 ≤ xi < yi ≤ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.

Consider all the chemicals numbered from 1 to n in some order.

Output

Print a single integer — the maximum possible danger.

Sample Input

Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output

4


并查集水题,但比赛时没做出来- -!


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int init[100010],t[100010],p[100010];

int s=0;

int fi(int r)
{
    return r==init[r]?r:init[r]=fi(init[r]);
}

void mer(int x,int y)
{
    int a=fi(x);
    int b=fi(y);
    if(a>b)
        init[a]=b;
    else
        init[b]=a;
}

int main()
{
    int n,m,i,j,k,x,y;
    while(scanf( "%d%d",&n,&m)!=EOF )
    {
        s=0;
        for(i=1;i<=n;i++)
            init[i]=i;
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&x,&y);
            mer(x,y);
        }
        for(i=1;i<=n;i++)//刚开始写的时候,居然天真的把s++,写到了合并里。。
        {
            if(init[i]!=i)
            {
                s++;
            }
        }
        s=n-s;
        printf("%.0lf\n",pow(2,s));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值