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
1 0
1
2 1 1 2
2
3 2 1 2 2 3
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;
}