CodeForces 445B DZY Loves Chemistry
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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
1 0
1
2 1
1 2
2
3 2
1 2
2 3
4
Hint
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).
#include<stdio.h> int pre[]; int Find(int x)
{
int r=x;
while(r!=pre[r])
r=pre[r];
int i=x,j;
while(pre[i]!=r)
{
j=pre[i];
pre[i]=r;
i=j;
}
return r;
} void join(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx!=fy)
pre[fy]=fx;
} int main()
{
int n,m,i,j,x,y;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=;i<=n;i++)
pre[i]=i;
for(i=;i<=m;i++)
{
scanf("%d %d",&x,&y);
join(x,y);
}
int t[]={},k=;
for(i=;i<=n;i++)
t[Find(i)]=;
for(i=;i<=n;i++)
if(t[i]==)
k++;
long long ans=;
for(i=;i<=n-k;i++)
ans=ans*;
printf("%I64d\n",ans);
}
return ;
}
CodeForces 445B DZY Loves Chemistry的更多相关文章
- CodeForces 445B. DZY Loves Chemistry(并查集)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...
- codeforces 445B. DZY Loves Chemistry 解题报告
题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...
- CodeForces 445B DZY Loves Chemistry (并查集)
题意: 有N种药剂编号 1 ~ N,然后有M种反应关系,这里有一个试管,开始时危险系数为 1,每当放入的药剂和瓶子里面的药剂发生反应时危险系数会乘以2,否则就不变,给出N个药剂和M种反应关系,求最大的 ...
- CF 445B DZY Loves Chemistry(并查集)
题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second memory limit per test:256 megabytes D ...
- CodeForces - 445B - DZY Loves Chemistry-转化问题
传送门:http://codeforces.com/problemset/problem/445/B 参考:https://blog.csdn.net/littlewhite520/article/d ...
- Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #254 (Div. 2):B. DZY Loves Chemistry
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces 444C DZY Loves Colors(线段树)
题目大意:Codeforces 444C DZY Loves Colors 题目大意:两种操作,1是改动区间上l到r上面德值为x,2是询问l到r区间总的改动值. 解题思路:线段树模板题. #inclu ...
随机推荐
- Vim篇
Vim编辑器中的一些常用命令: 1:shift+* , 选取光标所在处的整个字符,并查找.(十分方便),快捷键gd 2:set nu , 显示各行行号,使得基于行的命令更方便. 3:shift+% , ...
- yii Html中的a标签使用
1.use yii\helpers\Html; <?php echo Html::a('编辑',['edit','id'=>$info['goods_id']])?> 2.有确认框的 ...
- ASP.NET MVC的TempData(转载)
本文章基于ASP.NET MVC Preview5. ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个控制器Action间传递临时的数据或者给View传递一些临时的数据,相信 ...
- Sql server analysis service 通过IIS连接时的最大连接数问题
做过SSAS项目的大部分人都应该知道SSAS是可以通过在IIS上建立代理站点的方式来建立远程连接的,这样可以绕过连接到SSAS时需要在同一个域环境下通过域用户来验证的问题,这样即使连接到SSAS的客户 ...
- html5 canvas 笔记四(变形 Transformations)
绘制复杂图形必不可少的方法 save() 保存 canvas 状态 restore() 恢复 canvas 状态 Canvas 的状态就是当前画面应用的所有样式和变形的一个快照. Canvas 的状态 ...
- Git的搭建和使用技巧完整精华版
[Git使用技巧] 1.把一个已经存在于版本库中的文件加入忽略提交文件(.gitignore)中,需要如下代码: git rm --cached [文件路径] 例如: git rm --cached ...
- SQL基础教程
第一范式: 列仅包含原子值: 没有重复的组. 第二范式: 满足第一范式: 非部分函数依赖.(如果组合键中任何一列值改变,将导致非键列的值需要被更新) 那么,主键是一列(不是组合的)满足第二范式:所有列 ...
- String 类的常用字符串方法
public class Page106 { /** * 字符串练习第五章 * @param args */ public static void main(String[] args) { Stri ...
- 20151124001 关闭C#主窗体弹出是否关闭对话框
关闭C#主窗体弹出是否关闭对话框 private void Frm_Main_FormClosing(object sender, FormClosingEventArgs e) { ...
- 周赛-The Number Off of FFF 分类: 比赛 2015-08-02 09:27 3人阅读 评论(0) 收藏
The Number Off of FFF Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...