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 ...
随机推荐
- python3使用csv模块读写csv文件
python3使用csv模块读写csv文件 读取csv文件: import csv #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() ...
- andriod之应用内置浏览器 webview
参考:http://my.eoe.cn/694183/archive/10476.html http://blog.csdn.net/it_ladeng/article/details/8136534 ...
- 对OpenGL的GLFrame框架进行的扩展截至2014年11月29日
框架源自<OpenGL游戏编程>,增加了此框架的部分功能.其中有些小错误,尤其是MD2模型的那章,给出的框架只支持载入一个BOSS,当再载入一个BOSS时,就会发现两个模型的帧速会乱套. ...
- Floyd算法核心代码证明
Flody 大家都知道这个最终模版: for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i ...
- c++实现mlp神经网络
之前一直用theano训练样本,最近需要转成c或c++实现.在网上参考了一下其它代码,还是喜欢c++.但是看了几份cpp代码之后,发现都多少有些bug,很不爽.由于本人编码能力较弱,还花了不少时间改正 ...
- iOS 开发 证书总结 开发证书和生产证书的区别
IOS开发 证书总结 开发者证书 ------>> 开发证书是你在真机推送时 用得, 生产证书是app 上架之后 推送给用户用的 首先你必须获得apple开发者证书,上图这个文件就是 ...
- PHP使用PHPExcel删除Excel单元格指定列的方法是怎样
有一个系统仅公司内部和外部经销商使用,在一个导出功能中公司内部员工跟外部经销商导出的列是不一样的(某些数据是不能提供给经销商的)因为导出的数据都是一样的(某些列外数据外部没有)因此并没有单独处理,而是 ...
- mysql语句
查询字段长度:SELECT MAX(LENGTH(pd)) FROM `table` where id=2;来检查当前表中字段的字符集设置.show full fields from tableNam ...
- awk 手册--【转载】
1. 前言 有关本手册 : 这是一本awk学习指引, 其重点着重于 : l awk 适于解决哪些问题 ? l awk 常见的解题模式为何 ? 为使读者快速掌握awk解 ...
- 不定参数的传递VA_LIST的用法
VA_LIST的用法:(1)首先在函数里定义一具VA_LIST型的变量,这个变量是指向参数的指针: (2)然后用VA_START宏初始化变量刚定义的VA_LIST变量,使其指向第一个可 变参数的地址: ...