转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://codeforces.com/problemset/problem/445/B

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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 test(s)
input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4
Note

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 <cstdio>
#include <cmath>
int father[1005];
int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
}
void Union(int x,int y)
{
int f1=find(x);
int f2=find(y);
if(f1!=f2)
{
father[f2]=f1;
}
}
int main()
{
int n,m,a,b;
int i, j;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i = 1 ; i <=n ; i++ )
father[i] = i ;
if(m == 0)
{
printf("1\n");
continue;
}
int k=0;
for(i = 0 ; i < m ; i++ )
{
scanf("%d%d",&a,&b);
Union(a,b);
}
__int64 msum = 1;
for(i=1 ; i <= n ; i++)
if(father[i]==i)
k++;
int ans = n - k;
msum = pow(2,ans);
printf("%I64d\n",msum);
}
return 0 ;
}

CodeForces 445B. DZY Loves Chemistry(并查集)的更多相关文章

  1. CodeForces 445B DZY Loves Chemistry

    DZY Loves Chemistry Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  2. CodeForces 445B DZY Loves Chemistry (并查集)

    题意: 有N种药剂编号 1 ~ N,然后有M种反应关系,这里有一个试管,开始时危险系数为 1,每当放入的药剂和瓶子里面的药剂发生反应时危险系数会乘以2,否则就不变,给出N个药剂和M种反应关系,求最大的 ...

  3. codeforces 445B. DZY Loves Chemistry 解题报告

    题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...

  4. UOJ_14_【UER #1】DZY Loves Graph_并查集

    UOJ_14_[UER #1]DZY Loves Graph_并查集 题面:http://uoj.ac/problem/14 考虑只有前两个操作怎么做. 每次删除一定是从后往前删,并且被删的边如果不是 ...

  5. CF 445B DZY Loves Chemistry(并查集)

    题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second     memory limit per test:256 megabytes D ...

  6. CodeForces - 445B - DZY Loves Chemistry-转化问题

    传送门:http://codeforces.com/problemset/problem/445/B 参考:https://blog.csdn.net/littlewhite520/article/d ...

  7. UOJ14 DZY Loves Graph 并查集

    传送门 题意:给出一张$N$个点,最开始没有边的图,$M$次操作,操作为加入边(边权为当前的操作编号).删除前$K$大边.撤销前一次操作,每一次操作后询问最小生成树边权和.$N \leq 3 \tim ...

  8. cf444E. DZY Loves Planting(并查集)

    题意 题目链接 Sol 神仙题啊Orzzzzzz 考场上的时候直接把树扔了对着式子想,想1h都没得到啥有用的结论. 然后cf正解居然是网络流??出给NOIP模拟赛T1???¥%--&((--% ...

  9. 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 ...

随机推荐

  1. OA系统权限管理设计方案

    (转)OA系统权限管理设计方案 OA系统权限管理设计方案     不同职责的人员,对于系统操作的权限应该是不同的.优秀的业务系统,这是最基本的功能.     可以对“组”进行权限分配.对于一个大企业的 ...

  2. jquery选择器用法笔记(第二部分)

    今天继续讲讲jquery选择器的更多用法,希望能给大家带来帮助. 9.$("ul li:eq(3)")  --  列表中的第四个元素(index 从 0 开始) :eq() 选择器 ...

  3. js cookie实例

    什么是cookie:           △ 用来保存用户信息:用户名.密码... ...           △ 同一网站共享一套cookie,大小有限,保存时间           △ 使用doc ...

  4. Android之PowerManager&BatteryManager

    PowerManager是Android平台中用于管理控制设备电源状态.重启.休眠状态.唤醒等,使用该API会影响到电池的待机时间,所以无非必要,一般不要使用. 在PowerManager中有几个比较 ...

  5. 关于NLPIR应用在KETTLE中的探索

    一:什么是NLPIR? NLPIR汉语分词系统(自然语言处理与信息检索共享平台),主要功能包括中文分词:词性标注:命名实体识别:用户词典功能:支持GBK编码.UTF8编码.BIG5编码.新增微博分词. ...

  6. FM遇到错误RQP-DEF-0354和QE-DEF-0144

    版本:Cognos 10.2.1 系统:Win10 操作过程:在FM调用了一个存储过程,其中引用了前端page页面的参数如下图所示,在验证和保存查询主题的时候一直提示参数没有替换值,错误 信息如下图所 ...

  7. cognos report利用文本框提示优化日期维度

    为了尽量减少手工对日期维度的维护,在日期维度表中年份已经到了2099年,把年份作为下拉框或者月份作为下拉框的时候,选择起来颇为麻烦(当然也可以在此基础之上设置默认为当前月) 如图:提示页面以及html ...

  8. Linux:编译动态库时遇到的错误relocation R_X86_64_32 against `a local symbol'

    编译动态库时遇到如下错误: ... ... relocation R_X86_64_32 against `a local symbol' can not be used when making a ...

  9. rapidxml 节点加入另一个xml

    void TestRapidXml() { ]; sprintf(xmlContent,"<root><head>aaa</head><body&g ...

  10. 微信小程序-开发入门(一)

    微信小程序已经火了一段时间了,之前一直也在关注,就这半年的发展来看,相对原生APP大部分公司还是不愿意将主营业务放到微信平台上,以免受制于腾讯,不过就小程序的应用场景(用完即走和二维码分发等)还是很值 ...