DZY Loves Chemistry
2015-08-06 15:16
330 查看
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
Output
Input
Output
Input
Output
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,如果没有反应的危险值就不变
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
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,如果没有反应的危险值就不变
#include<iostream> #include<cstdio> #include<cmath> using namespace std; #define N 55 int f ; int found(int a) { if(f[a] != a) f[a] = found(f[a]); return f[a]; } int main() { int n, m, a, b; while(scanf("%d%d", &n, &m) != EOF) { for(int i = 1; i <= n; i++) f[i] = i; int x = n; while(m--) { scanf("%d%d", &a, &b); int na = found(a), nb = found(b); f[na] = nb; } for(int i = 1; i <= n; i++) { if(f[i] == i) // 如果根节点是他自己,和别人没关系,放进去就不反应,就少乘一个2,有几颗树,就有几个根节点放进去的时候是不反应的 x--; } long long ans = pow(2, x); printf("%lld\n", ans); } return 0; }
相关文章推荐
- nginx 添加404页面后,仍然提示file not found
- iOS开发系列--并行开发其实很容易
- 20个数据可视化(数据视觉化)工具
- Sublime 3新建文件快速生成Html头部信息
- JS判断设备屏幕方向
- Dijkstra 最短路径算法详解 无向图
- MyBatis学习总结一
- Oracle下一条SQL语句的优化
- TabHost实现底部菜单
- android 图片缩放
- Java笔试题及答案
- 创建javascript对象的方法
- 选择区间不相交问题
- Jena 简介:通过 Jena Semantic Web Framework 在 Jave 应用程序中使用 RDF 模型
- BloomFilter–大规模数据处理利器(转)
- WebStorm注册码
- 提问的智慧
- JS indexOf()方法
- nginx 连接限制新语法
- Java 连接数据库的JDBC