CodeForces 445B. DZY Loves Chemistry(并查集)
转载请注明出处: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.
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
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(并查集)的更多相关文章
- CodeForces 445B DZY Loves Chemistry
DZY Loves Chemistry Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- CodeForces 445B DZY Loves Chemistry (并查集)
题意: 有N种药剂编号 1 ~ N,然后有M种反应关系,这里有一个试管,开始时危险系数为 1,每当放入的药剂和瓶子里面的药剂发生反应时危险系数会乘以2,否则就不变,给出N个药剂和M种反应关系,求最大的 ...
- codeforces 445B. DZY Loves Chemistry 解题报告
题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...
- UOJ_14_【UER #1】DZY Loves Graph_并查集
UOJ_14_[UER #1]DZY Loves Graph_并查集 题面:http://uoj.ac/problem/14 考虑只有前两个操作怎么做. 每次删除一定是从后往前删,并且被删的边如果不是 ...
- 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 ...
- UOJ14 DZY Loves Graph 并查集
传送门 题意:给出一张$N$个点,最开始没有边的图,$M$次操作,操作为加入边(边权为当前的操作编号).删除前$K$大边.撤销前一次操作,每一次操作后询问最小生成树边权和.$N \leq 3 \tim ...
- cf444E. DZY Loves Planting(并查集)
题意 题目链接 Sol 神仙题啊Orzzzzzz 考场上的时候直接把树扔了对着式子想,想1h都没得到啥有用的结论. 然后cf正解居然是网络流??出给NOIP模拟赛T1???¥%--&((--% ...
- 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 ...
随机推荐
- relatedTarget、fromElement、toElement相关元素
在发生mouseover和mouseout事件时,还会涉及更多的元素.这两个事件都会涉及把鼠标指针从一个元素的边界之内移到另一个元素边界之内.对mouseover事件而言,事件的主目标是获得光标的元素 ...
- HTML标签 闭合还是不闭合?
你在写 HTML5 代码的时候,是否纠结过应该写 <br /> 还是 <br>,是写 <input /> 还是写 <input>.写 <scrip ...
- cognos report在做同比时遇到的问题解决方法
本例就拿简单的一个模型作为测试: 订单中包括日期key,商品类型key 现在要实现每月的订单数,以及去年同期的订单数: step1:新建2个数据项 本月,去年同月 本月: [每日订单数据分析].[日期 ...
- MATLAB数据处理快速学习教程
转自:http://blog.csdn.net/abcjennifer/article/details/7706581 本篇内容集合了MATLAB中的基本操作.数据存储与计算.数据的直线与曲线拟合与画 ...
- Qt5 for Android: incompatible ABI
I recently installed Qt5 and works like a charm for API 17 and armeabi-v7a.But I added second AVD ...
- jquery文字填写自动高度
下面开始写一个jquery插件 (function($){ $.fn.autoTextarea = function(options) { var defaults={ maxHeight:null, ...
- 100款免费的旅游素材(PSD)
本地下载 素材一直都是网页设计者们大爱,不同的类型和设计总能带给人们新鲜感.今天带来的这100个免费的旅行素材,PSD格式,可以自定义色彩.一定可以带给你们非常惊喜!
- springboot项目启动报错
启动springboot项目报错: NoSuchMethodError: org.apache.tomcat.util.scan.StandardJarScanner.setJarScanFilter ...
- secureCRT简单设置(学习笔记二)
菜鸟记录. 一.更改终端类型 选项-全局选项-默认会话-编辑默认设置-终端-仿真-右侧选择类型,下方设置缓冲区大小 二.设置字体和外观 上方窗口外观-右侧设置-字体设置字体类型大小,下面光标可以设置光 ...
- 算法笔记_196:历届试题 剪格子(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+|10* 1|52|+--****--+|20|30* 1|**** ...