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. 简单题(K-D Tree)

    简单题不简单-- 我们把单点加操作改成插入一个权值为增加量的点,将问题转化成询问一个矩阵中所有点的和,用 \(K-D\ Tree\) 维护,时间复杂度 \(O(n\sqrt{n})\) \(Code\ ...

  2. isBalanced函数实现

    原文:从一道面试题谈起,作者:360奇舞团 刘观宇 题目: 创建一个函数来判断给定的表达式中的大括号是否闭合,返回 true/false,对于空字符串,返回 true var expression = ...

  3. 13、最新安卓Xamarin绑定相关填坑之旅

    今天群里面有兄弟伙说第三方库用不起.说实话在我觉得第三方库能成功的几率大于90% 除了极少数恶心的库以外. 绝大部分第三方库都还是可以绑定好的 https://github.com/youzan/Yo ...

  4. 写一个MySql存储过程实现房贷等额本息还款计算(另外附javascript代码)

    写一个MySql存储过程实现房贷等额本息还款计算 MySql存储过程代码如下: DROP procedure IF EXISTS `calc_equal_interest_proc`; DELIMIT ...

  5. 阿里云负载不支持 WebSocket 协议与 WSS 和 Nginx 配置问题

    WebSocket 是 HTML5 下一种新的协议.它实现了浏览器与服务器全双工通信,能更好的节省服务器资源和带宽并达到实时通讯的目的.它与HTTP一样通过已建立的TCP连接来传输数据,但是它和HTT ...

  6. js便签笔记(5)——Dean Edwards大牛的跨浏览器AddEvent()设计(不知道是不是jQuery事件系统的原型)

    1. 前言: 在看Aaron的jquery源码解读时候,看到事件系统那块,作者提到了Dean Edwards的添加事件的设计,于是就点进去看了看.首先让我吃惊的是,代码非常少,寥寥几十行,非常简单.于 ...

  7. CentOS下SSH远程免密登录服务器

    .5服务器上配置,通过ssh远程免密登录192. 1.安装SSH,此处省略 2.生成公钥和私钥,生成的秘钥默认在/root/.ssh/文件夹里面 [root@localhost ~ ::&&a ...

  8. docker(三)docker镜像和镜像发布方法

    一.从公网docker hub 拉取image ~ # 搜索docker search centos~ » docker pull centos admin@steven- Using default ...

  9. java监听器、定时器的使用

    1.监听器 在web.xml配置 <!-- 时间任务 --> <listener> <listener-class> com.hk.common.timer.Tim ...

  10. Web前后端分离知识整理

    Web研发模式的演变 职责分离(减少扯皮,开发效率),代码分离(可维护性) 简单明快的早期时代 后端为主的 MVC 时代 Ajax 带来的 SPA 时代 前端为主的 MV* 时代 Node 带来的全栈 ...