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内 ...
随机推荐
- Node.js 网页爬虫再进阶,cheerio助力
任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...
- 【Android开发-6】了解内情,我们须要一些调试和測试手段
前言:人生不可能十全十美,总会有些遗憾存在,经历过遗憾,我们才懂的什么是生活. 程序也一样.追求完美,就必定会有经历bug存在的时候. 经历过不断的bug磨练.我们技术才会不断的成长.对于调试bug, ...
- php 记录图片浏览次数次数
<?php $url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']; $la ...
- PHP面试题及答案解析(3)—MySQL数据库
1.mysql_num_rows()和mysql_affected_rows()的区别. mysql_num_rows()和mysql_affected_rows(),这两个函数都作用于 mysql_ ...
- mysql更改表结构:添加、删除、修改字段、调整字段顺序
添加字段: alter table `user_movement_log` Add column GatewayId int not null default 0 AFTER `Regionid` ( ...
- jquery字符串转json
var data; var json='[{"CityId":18,"CityName":"西安","ProvinceId&quo ...
- java线程模型Master-Worker
这样的模型是最经常使用的并行模式之中的一个,在Nginx源代码中有涉及到有想看的能够去这个大神的博客了解一下http://blog.csdn.net/marcky/article/details/60 ...
- 在多点环境下使用cas实现单点登陆及登出
CAS 介绍 CAS 是 Yale 大学发起的一个开源项目,旨在为 Web 应用系统提供一种可靠的单点登录方法,CAS 在 2004 年 12 月正式成为 JA-SIG 的一个项目.CAS 具有以下特 ...
- 2018,从AI看安卓生态的变革
AI的发展与影响 与传统技术不同的是,AI技术算法清晰,优化目标明确,基础技术成熟,使得一众中小创企也看到了市场的机会.2017年中国企业动作频频,在自动驾驶,智能安防,智慧城市等领域都取得了不俗的成 ...
- nginx 的uri、request_uri 区别
在nginx中有几个关于uri的变量,包括$uri $request_uri $document_uri,下面看一下他们的区别 : $request_uri: /stat.php?id=1585378 ...