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 ...
随机推荐
- c语言 typedef
在C和C++编程语言中,typedef是一个关键字.它用来对一个资料类型取一个新名字.目的是为了使源代码更易于阅读和理解. 来看以下程式码: int coxes; int jaffa; ... c ...
- android 设置Button或者ImageButton的背景透明 半透明 透明
Button或者ImageButton的背景设为透明或者半透明 半透明<Button android:background="#e0000000" ... /> 透明 ...
- js正则函数match、exec、test、search、replace、split使用介绍集合
match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...
- linux端口
1.查看开放的端口 netstat -anp 来查看哪些端口被打开. 注:加参数'-n'会将应用程序转为端口显示,即数字格式的地址,如:nfs->2049, ftp->21,因此可以开启两 ...
- 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- arm驱动linux异步通知与异步IO【转】
转自:http://blog.csdn.net/chinazhangzhong123/article/details/51638793 <[ arm驱动] linux异步通知与 异步IO> ...
- 深入浅出Windows Phone 8应用开发
刚刚拿到<深入浅出Windows Phone 8应用开发>这本书,开始捣鼓我的Nokia Lumia 925T,已经有开发一个属于自己的App的想法了,计划先不公布了,等我这个App上线了 ...
- HDU 3652:B-number(数位DP)
http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意:求数位含有13和可以被13整除的数字个数. 思路:记录3种状态: st == 0 表示 从最高位到第 ...
- 如何修改ECShop发货单查询显示个数
使用ecshop的朋友都知道,商城首页调用的发货单查询,默认显示的10个.很多朋友想修改它的数量,可是在后台管理却找不到相应的地方,这个修改和显示排行榜的数量修改方法不一样.排行榜是可以在后台修改的, ...
- recycleview + checkbox 实现单选
使用map集合记录checkbox的选中状态 private HashMap<Integer,Boolean> positionMap; positionMap = new HashMap ...