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.

Sample test(s)
input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4

思路:依据反应关系找到全部的集合,再在集合中求得结果(由于n<=50。所以用__int64)

代码:

#include <stdio.h>
int father[55];
int find(int x)
{
if (father[x] == x)
return x;
else
return (father[x] = find(father[x]));
}
void merge(int a, int b)
{
int x, y;
x = find(a);
y = find(b);
if (x != y)
father[x] = y;
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = 1; i <= n; i++)
father[i] = i;
while (m--){
int x, y;
scanf("%d%d", &x, &y);
merge(x, y);
}
__int64 ans = 1;
for (int i = 1; i <= n; i++){
int fa = father[i];
if (fa == i){
int s = 2;
for (int j = 1; j <= n; j++)
if (find(j) == fa&&i != j){
ans = ans*s;
}
}
}
printf("%I64d\n", ans);
}
return 0;
}

Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry的更多相关文章

  1. [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry

    链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...

  2. Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

    题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的 ...

  3. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  4. Codeforces Round #254 (Div. 1) D - DZY Loves Strings

    D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...

  5. Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力

    D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...

  6. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

  7. Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题

    A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...

  8. Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs

    题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...

  9. Codeforces Round #254 (Div. 1) C DZY Loves Colors

    http://codeforces.com/contest/444/problem/C 题意:给出一个数组,初始时每个值从1--n分别是1--n.  然后两种操作. 1:操作 a.b内的数字是a,b内 ...

随机推荐

  1. Go语言格式化字符

    https://github.com/polaris1119/The-Golang-Standard-Library-by-Example/blob/master/chapter01/01.3.md

  2. Odoo12 重大改变

    Table of Contents 重构的功能 ORM 数据导入 库存 库存规则 MRP 多步路线 新功能 IoT     Odoo12 预计 2018/10 在 Odoo experience 20 ...

  3. 捕获php请求内容

    通过将phpinfo()打印到文件实现 <?php echo str_pad('', 1024);//使缓冲区溢出 ob_start();//打开缓冲区 phpinfo(); $string = ...

  4. discuz密码生成

    Discuz的密码加密算法其实就是两次MD5加密,首先用明文进行一次加密,之后随机生成一个salt,再把第一次的密文后面添加salt作为明文再进行一次MD5加密.salt保存在uc_members表里 ...

  5. 监听EditText字数

    editContent.addTextChangedListener(new TextWatcher() { private CharSequence temp;private int editSta ...

  6. [原创]个人工具 - 对APK极限压缩并对齐的工具(58.ReExtremeZipAndAlignAPK)

    2014年9月13日:RootExplorer_v2.21.1_Aligned对齐后:在华为U8860(2.3.6).酷派8150S(4.0.3)上安装滑动列表时,崩溃.原版本(已对齐)则OK.(不知 ...

  7. reveal end of document

     window - Preferences - Run/Debug - Console 将 Console buffer size (characters)设置大一点

  8. gulp配置,实例演示

    项目完成后的目录 我们所需要的插件为:gulp-minify-css gulp-concat gulp-uglify gulp-rename del 如下图所示,完成后的项目目录结构: 附加,获取pa ...

  9. 【SQLServer2008】之Win10 安装 SQL Server 2008

    查看安装步骤链接: http://jingyan.baidu.com/article/1709ad8092be974634c4f0e7.html

  10. 打造一个高逼格的android开源项目——小白全攻略 (转)

    转自:打造一个高逼格的android开源项目 小引子 在平时的开发过程中,我们经常会查阅很多的资料,最常参考的是 github 的开源项目.通常在项目的主页面能看到项目的简介和基本使用,并且时不时能看 ...