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. 【文文殿下】WC2019游记

    Day0 今天早上三点半才睡着,五点起床,前往省城郑州.与省实验常老师汇合,坐上高铁,下午三点半多才到广州二中. 下午随便找了一个教室进去敲一敲代码,发现自己越来越菜了. 和一大堆网上的dalao面基 ...

  2. 写vue项目时候 零星的笔记

    1,挂载也可以用 .$mount() 2,子组件中通过this.$root拿到实例的数据.截图中是子组件中

  3. linux的Yum软件包管理工具

    Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...

  4. Ubutntu安装docker启动报Removed /etc/systemd/system/docker.service.

    Ubutntu安装docker启动报Removed /etc/systemd/system/docker.service.的错误,只需要执行以下三条命令. systemctl unmask docke ...

  5. POJ 2636

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  6. 用AOP思想改造一个服务器的数据存储

    背景是有一个游戏服务器一直以来都是写SQL的, 后来改过一段时间的redis, 用的是别的员工写的类orm方式将实体类型映射成各种key-value对进行写入, 但是仍有一个缺点就是需要在增\删\改的 ...

  7. MySQL 5.5主从复制(Replication)

    简介 MySQL的主从复制(Replication),可以保持两台MySQL数据库的内容一致.因为其同步过程是异步的,所以备份数据库上做任何操作,都不会影响主数据库的性能.对游戏后台数据库而言,这是一 ...

  8. coursera 视频总是缓冲或者无法观看,有什么方法解决?

    win7电脑,三个步骤: (1)修改hosts文件,地址如下: C:\Windows\System32\drivers\etc,然后以文本格式打开hosts. (2)将如下内容复制到文件末尾 52.8 ...

  9. (转)centos 7 Tomcat 8.5 的安装及生产环境的搭建调优

    原文:https://www.cnblogs.com/linhankbl/articles/9149804.html#top JVM菜鸟进阶高手之路七(tomcat调优以及tomcat7.8性能对比) ...

  10. Bridge桥接模式(结构型模式)

    现有一个需求,一个游戏系统需要构建不同风格的房屋,暂不考虑其他设计模式,需要能实现在PC端.移动端....等等多个平台的构建.最简单的实现方式如下: /// <summary> /// 房 ...