Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry
1 second
256 megabytes
standard input
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.
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.
Print a single integer — the maximum possible danger.
1 0
1
2 1
1 2
2
3 2
1 2
2 3
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的更多相关文章
- [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry
链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...
- Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)
题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂 总共有m对试剂能反应,按不同的 ...
- 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 ...
- Codeforces Round #254 (Div. 1) D - DZY Loves Strings
D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...
- 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 ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题
A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...
- 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内 ...
随机推荐
- GROUP BY和HAVING 以及mysql中常用的日期函数
一.mysql中的GROUP BY和HAVINGGROUP BY常见的是和聚合函数(SUM,MIN,MAX,COUNT)搭配使用. 比如:SELECT category,SUM(money) AS ` ...
- Android中Activity的生命周期图
- 浅谈struts2标签中的2个非经常常使用的标签的使用方法(radio和select)
1.如图所看到的我们须要在前台的页面通过radio和select将相应的数据库中的数据显示到选项其中,这也是我们做项目中常常须要做的,动态的显示,而不是静态的显示. 首先我们须要在页面中导入strut ...
- 【SpringMVC学习06】SpringMVC中的数据校验
这一篇博文主要总结一下springmvc中对数据的校验.在实际中,通常使用较多是前端的校验,比如页面中js校验,对于安全要求较高的建议在服务端也要进行校验.服务端校验可以是在控制层conroller, ...
- MongoDB在Win10下的安装
原文地址:http://blog.csdn.net/polo_longsan/article/details/52430539 1.下载MongoDB在windows下的安装文件 首先去官网https ...
- iostat命令分析磁盘io
1.安装 yum install sysstat 2.参数 建议将man 文档看一遍 3.简单判断io状况 iostat -d -k 2 -x Device: rrqm/s wrqm/s r/s w/ ...
- 【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)
[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS Memory Limit ...
- 怎样新建Quartusproject—FPGA新手教程
这一章我们来实现第一个FPGAproject-LED流水灯.我们将通过流水灯例程向大家介绍一次完整的FPGA开发流程,从新建project,代码设计,综合实现.管脚约束,下载FPGA程序. 掌握本章内 ...
- SpringBoot启动流程分析(五):SpringBoot自动装配原理实现
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- 利用SQL server 的复制功能分散用户访问服务器的负载
先来了解一下一个基本的关于复制的概念. 什么是复制? 复制就是把数据的多个拷贝(复制品)分发到公司中的各个服务器中,通过复制为多台服务器提供相同的数据.这样用户就可以在不同服务器中访问同样的信息. 对 ...