CF 445B DZY Loves Chemistry(并查集)
题目链接: 传送门
DZY Loves Chemistry
time limit per test:1 second memory limit per test:256 megabytes
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
2 1
1 2
3 2
1 2
2 3
Sample Output
1
2
4
思路:
题目大意:给你n种化学物品,其中有的化学物品能反应,若加入一种化学物品能与试管中已有的物品反应,则危险值×2,问最大的危险值。
最大危险值很明显是2^(n-v),其中“v”为连通块的个数。比如1-6六个数,1-3、4-5、6分为三个连通块,显然只有在同一个连通块里的化学物品能反应。
#include <cstdio>
using namespace std;
int n, m, fa[100], x, y;
int gf(int x)
{
if (fa[x] != x) fa[x] = gf(fa[x]);
return fa[x];
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) fa[i] = i;
while (m--)
{
scanf("%d%d", &x, &y);
fa[gf(x)] = gf(y);
}
long long ans = (1LL << n);
for (int i = 1; i <= n; i++)
if (gf(i) == i) ans /= 2;
printf("%I64d\n", ans);
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef __int64 LL;
int ans[55];
int N,M;
void init()
{
for (int i = 1;i <= N;i++)
{
ans[i] = i;
}
}
int main()
{
while (~scanf("%d%d",&N,&M))
{
int u,v;
LL res = 1;
memset(ans,0,sizeof(ans));
init();
while (M--)
{
scanf("%d%d",&u,&v);
while (u != ans[u])
{
u = ans[u];
}
while (v != ans[v])
{
v = ans[v];
}
if (u != v)
{
res *= 2;
}
ans[v] = u;
}
printf("%I64d\n",res);
}
return 0;
}
CF 445B DZY Loves Chemistry(并查集)的更多相关文章
- UOJ_14_【UER #1】DZY Loves Graph_并查集
UOJ_14_[UER #1]DZY Loves Graph_并查集 题面:http://uoj.ac/problem/14 考虑只有前两个操作怎么做. 每次删除一定是从后往前删,并且被删的边如果不是 ...
- CodeForces 445B. DZY Loves Chemistry(并查集)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...
- CodeForces 445B DZY Loves Chemistry
DZY Loves Chemistry Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- CF 445B(DZY Loves Chemistry-求连通块)
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CodeForces 445B DZY Loves Chemistry (并查集)
题意: 有N种药剂编号 1 ~ N,然后有M种反应关系,这里有一个试管,开始时危险系数为 1,每当放入的药剂和瓶子里面的药剂发生反应时危险系数会乘以2,否则就不变,给出N个药剂和M种反应关系,求最大的 ...
- cf444E. DZY Loves Planting(并查集)
题意 题目链接 Sol 神仙题啊Orzzzzzz 考场上的时候直接把树扔了对着式子想,想1h都没得到啥有用的结论. 然后cf正解居然是网络流??出给NOIP模拟赛T1???¥%--&((--% ...
- UOJ14 DZY Loves Graph 并查集
传送门 题意:给出一张$N$个点,最开始没有边的图,$M$次操作,操作为加入边(边权为当前的操作编号).删除前$K$大边.撤销前一次操作,每一次操作后询问最小生成树边权和.$N \leq 3 \tim ...
- codeforces 445B. DZY Loves Chemistry 解题报告
题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...
- 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 ...
随机推荐
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- 海王星给你好看!FineUI v4.0公测版发布暨《你找BUG我送书》活动开始(活动已结束!)
<FineUI v4.0 你找BUG我送书>活动已结束,恭喜如下三位网友获得由 FineUI 作者亲自翻译的图书<jQuery实战 第二版>! 奋斗~ 吉吉﹑ purplebo ...
- 整合Apache与PHP教程
Apache下载安装完成后,PHP下载解压后,最重要的是如何将他们连接起来,就是整合到一起,让它们之间有联系,笔者根据自己多次配的经验和帮学弟学妹配时他们的理解程度整理了一个比较详细易理解的版本,下面 ...
- DOM与CSS样式表
在前文 <DOM与元素节点内联样式>中我们了解了用 DOM 提供的接口操作元素节点内联样式的方法,今天我们来学习一下如何用 DOM 操作 CSS 样式表. CSS 样式表概况 通过使用 H ...
- SD卡状态广播
SD状态发生改变的时候会对外发送广播.SD卡的状态一般有挂载.未挂载和无SD卡. 清单文件 一个广播接受者可以接受多条广播.这里在意图过滤器中添加是data属性是因为广播也需要进行匹配的.对方发送的广 ...
- 屠龙之路_战胜狮身人面怪物_SecondDay
第二天,少年们跋山涉水来到了恶龙山的山脚.前面有一座迷宫,守卫迷宫的是一只狮身人面的怪物,它出一个谜语让少年们猜,如果屠龙团猜不出答案就会被吃掉(如果你能猜出来,我就让你--),它问:"软件 ...
- android 之fragment创建
1.使用xml标签 1.1定义两个重要属性 <fragment android:id="@+id/fregment_top" android: ...
- Web Api单元测试写法
例如我们在Web Api项目中有个Controller public class SomeController : ApiController { public HttpResponseMessage ...
- Android中图片的处理(放大缩小,去色,转换格式,增加水印等)(转)
原文地址:http://menxu.lofter.com/post/164b9d_3ebf79 package com.teamkn.base.utils; import java.io.ByteAr ...
- Ubuntu14.04下MySQL的安装
1.输入 sudo apt-get install mysql-server 2.继续执行后,需要设定MySQL密码. 3.再次输入密码. 4.之后就安装成功了,输入mysql -u root -p进 ...