周赛2-CodeForces 445B-E 题
2015-08-08 15:50
281 查看
Description
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 Input
Input
1 0
Output
1
Input
2 11 2
Output
2
Input
3 21 22 3
Output
4
题意: 看化学反应的危险程度,初始时候为1,加一种反应就乘于2,用并查集就过了,当时一想,是不是多种反应就取乘于2啊,按照这个思路写了下,是错了,后来想到可能会成环,成环的话就多去乘了个2啊
并查集 A的代码
#include<stdio.h> #include<string.h> #include<stdlib.h> int ls[100]; int fd(int x) { return ls[x] == x ? x:ls[x]=fd(ls[x]); } void cb(int x,int y) { int xx = fd(x); int yy = fd(y); if(xx != yy) ls[xx]=yy; } int main() { int n, m; while(~scanf("%d%d",&n,&m)) { int i, j; long long k = 1; for(i = 0; i <= n; i++) ls[i] = i; for(i = 0; i < m; i++) { int u, v; scanf("%d%d",&u,&v); cb(u,v); } int c = 0; for( i = 1; i<= n; i++) { if(ls[i] == i) c++; } for(i = 0; i < n-c; i++) k=k*2; printf("%lld\n",k); } return 0; }
下面这个是乘2的那个Wa的代码
#include<stdio.h> #include<string.h> #include<stdlib.h> int map[155][155]; int main() { int n, m; while(~scanf("%d%d",&n,&m)) { memset(map,0,sizeof(map)); int i, j; long long k = 1; for(i = 0; i < m; i++) { int u, v; scanf("%d%d",&u,&v); if(!map[u][v]) { k=k*2; map[u][v] = 1; map[v][u] = 1; } } printf("%lld\n",k); } return 0; }
相关文章推荐
- 机房数据库之数据类型
- 一起学习 微服务(MicroServices)-笔记
- 使用函数对象(侯捷先生译为伪函数)实现二叉查找树
- uboot中控制台输入输出的实现
- JAVA中三种URL连接方法
- (Vanya and Lanterns)Codeforces Round #280 (Div. 2)B
- 数据挖掘工程师笔试及答案整理
- socket编程--socket基本概念
- 弹出框layer的使用封装
- android stduio 引入 .so
- Drazil and Tiles(CodeForces 515D)
- Hive Managed Table与 External Table区别
- 通用函数可变参数模板
- Android学习笔记之SlidingMenu
- 周赛-DZY Loves Chessboard
- 手机开发之web方式----启动程序检查更新版本
- Ubuntu录制gif动态图
- Linux C/C++计划Shell命令大杂烩(1)
- 应用spss可靠性分析软件
- Xcode使用git管理版本