B. DZY Loves Chemistry

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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.

Examples

input

1 0

output

1

input

2 1
1 2

output

2

input

3 2
1 2
2 3

output

4

Note

In the first sample, there's only one way to pour, and the danger won't increase.

In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.

In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).

能够相互反应的元素构成一个集合,用并查集找到有几个集合,从每个集合中各取一个放入杯中,然后不管从哪个集合取一个元素加入杯中都能够有办法翻倍,所以答案就是2的(元素个数减去集合个数)次方。

 //2017-08-06
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int fa[N], arr[N];
long long pow[N]; void init(){
pow[] = ;
for(int i = ; i < N; i++){
fa[i] = i;
pow[i] = pow[i-]*;
}
} int getfa(int x){
if(fa[x] == x)return x;
return fa[x] = getfa(fa[x]);
} void Merge(int a, int b){
int af = getfa(a);
int bf = getfa(b);
if(af != bf){
fa[bf] = af;
}
} int main()
{
int n, m, a, b;
while(scanf("%d%d", &n, &m)!=EOF){
init();
while(m--){
scanf("%d%d", &a, &b);
Merge(a, b);
}
int cnt = ;
for(int i = ; i <= n; i++)
if(fa[i] == i)cnt++;
if(m == )printf("1\n");
else printf("%lld\n", pow[n-cnt]);
} return ;
}
 import java.util.*;

 public class Main{
static final int N = 60;
static int[] fa = new int[N];
static int[] arr = new int[N];
static long[] pow = new long[N]; static void init(){
pow[0] = 1;
for(int i = 1; i < N; i++){
fa[i] = i;
pow[i] = pow[i-1]*2;
}
} static int getfa(int x){
if(fa[x] == x)return x;
return fa[x] = getfa(fa[x]);
} static void merge(int a, int b){
int af = getfa(a);
int bf = getfa(b);
if(af != bf)
fa[bf] = af;
} public static void main(String args[]){
int n, m, a, b;
Scanner cin = new Scanner(System.in);
while(cin.hasNext()){
n = cin.nextInt();
m = cin.nextInt();
init();
for(int i = 0; i < m; i++){
a = cin.nextInt();
b = cin.nextInt();
merge(a, b);
}
int cnt = 0;
for(int i = 1; i <= n; i++)
if(fa[i] == i)
cnt++;
if(m == 0)System.out.printf("1\n");
else System.out.println(pow[n-cnt]);
}
}
}

Codeforces445B(SummerTrainingDay06-N 并查集)的更多相关文章

  1. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  2. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  3. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  4. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  5. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  6. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  7. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  8. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  9. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

随机推荐

  1. 解压cpio.gz

    #gunzip 文件名.cpio.gz #cpio -idmv < 文件名.cpio

  2. Redis---事务和Wtach

    1. 概述 Redis通过 MULTI, EXEC / WATCH 等命令来实现事务. 事务提供一种将多个命令请求打包, 然后一次性.按顺序的执行多个命令的机制. 并且在事务执行期间, 服务器不会中断 ...

  3. Python语法基础练习

  4. odoo开发笔记--模型字段compute用法

    compute属性,实现的主要功能是,前端界面选择某个字段的时候,指定与该字段关联的其他字段可以关联,并联动的显示. 可以和inverse属性同时使用,不加inverse属性的话,前端界面的显示效果只 ...

  5. odoo 开发基础 -- postgresql重新启动、状态查看

    场景描述: 当遇到数据库不能正常访问的时候,我们首先想到的是,查看相关的告警日志,一般先查看系统的日志,然后查看数据库的日志,Linux平台下,postgresql的日志文件存放目录在如下路径: te ...

  6. 【原创】实现一个简单的邮件服务API

    经常在公司写一些内部小程序需要用到发邮件的功能,于是决定写一个邮件服务. 实现思路:以URL形式提供一个RESTful API 给客户端,客户端通过post请求把json格式的邮件信息发送到服务端,服 ...

  7. Android初识Helloworld

    在Eclipse+ADT中创建HelloWorld非常简单,直接按照导航下一步就可以了.本文重点不在如何创建,而在理解HelloWorld项目的文件. HelloWorld的目录结构有: src:存放 ...

  8. 杂谈:HTTP1.1 与 HTTP2.0 知多少?

    HTTP是应用层协议,是基于TCP底层协议而来. TCP的机制限定,每建立一个连接需要3次握手,断开连接则需要4次挥手. HTTP协议采用"请求-应答"模式,HTTP1.0下,HT ...

  9. Maven install [WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated to org.aspectj:aspectjrt:jar:1.5.4

    一.背景 最近在给项目打包的时候,在控制台老是出现一行警告:[WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated ...

  10. Spring Cloud 使用 FeignClient 启动报错

    我们首先来看一下报错信息 Description: Field businessFeignClient in com.ysc.service.BusinessConfigService require ...