CF 445B(DZY Loves Chemistry-求连通块)
1 second
256 megabytes
standard input
standard output
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).
建图,n个化学试剂为点,会反应的连边
非常easy发现,同一连通块。必定存在方案:除了第一个,剩下的都于已取的点的连边
易证该方案最优
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (50+10)
#define MAXM (MAXN*MAXN+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,m;
int edge[MAXM],next[MAXM],pre[MAXM],siz=1;;
void addedge(int u,int v){edge[++siz]=v;next[siz]=pre[u];pre[u]=siz;}
void addedge2(int u,int v){addedge(u,v);addedge(v,u);}
bool b[MAXN]={0};
int tot=0;
void dfs(int x)
{
tot++;b[x]=1;
Forp(x)
{
int v=edge[p];
if (!b[v]) dfs(v);
}
}
ll ans=1;
int main()
{
// freopen("Chemistry.in","r",stdin);
// freopen(".out","w",stdout);
scanf("%d%d",&n,&m);
For(i,m)
{
int x,y;
scanf("%d%d",&x,&y);
addedge2(x,y);
}
For(i,n)
if (!b[i])
{
tot=0;
dfs(i);
while (tot>1) ans*=2,tot--;
}
cout<<ans<<endl;
return 0;
}
CF 445B(DZY Loves Chemistry-求连通块)的更多相关文章
- 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
DZY Loves Chemistry Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- CodeForces 445B. DZY Loves Chemistry(并查集)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...
- 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来评估这 ...
- 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 ...
- cf445B DZY Loves Chemistry
B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CF 444C DZY Loves Physics(图论结论题)
题目链接: 传送门 DZY Loves Chemistry time limit per test1 second memory limit per test256 megabytes Des ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
随机推荐
- Angular JS中自定义标签 属性绑定的解释
看到自定义标签的文档时,文档作者解释的能力实在太弱,也可能是本人太笨,一下绕不过来. 看了一个stackoverflow答案,才算明白,在此贴出翻译,以供大家参考. .csharpcode, .csh ...
- CF848A From Y to Y
思路1: 每次贪心地选择满足i * (i - 1) / 2 <= k最大的i并从k中减去i * (i - 1) / 2,直至k为0.由于函数x * (x - 1) / 2的增长速度比2x要慢,所 ...
- Django--4、认证系统
cookie与session 概念 因http协议无法保存状态,但是又需要保存状态,所以有了cookie.它不属于http协议范畴 工作原理:相当于一段标识数据.在访问服务器产生标识内容(cookie ...
- UUID 生成32位随机串
java通过jdk自带的UUID,生成32位的随机串 private static String generate_UUID() { UUID uuid=UUID.randomUUID(); Stri ...
- [Testing][API][soapUI] 測試API 的軟體工具紀錄
soapUI 測試API 的軟體工具紀錄 http://files.cnblogs.com/vincentmylee/soapUIScript%E9%9C%80%E8%A6%81%E8%B3%87%E ...
- 字符集编码---3 Windows BOM
Windows平台下存储Unicode格式的文件时,会在文件头插入2到3字节的文件头.这个文件头就是BOM(Byte Order Marks). 这个文件头在Unicode中,无对应符号.所以不必担心 ...
- docker在ubuntu16.04下的安装及阿里云镜像的配置
1.获取最新版本的 Docker 安装包 anmin@ubuntu:~$ wget -qO- https://get.docker.com/ | sh 安装完成后有个提示: If you would ...
- Re0:DP学习之路 饭卡 HDU - 2546
解法 01背包变式,首先贪心的想一下如果要保证余额最小那么就需要用相减后最小的钱减去之前最大的价格,且得保证这个钱在5元以上 对于寻找如何减最多能包含在5元以上,这里用01背包 我们把价钱看做体积装进 ...
- Mybatis中collection和association的使用区别
1. 关联-association2. 集合-collection 比如同时有User.java和Card.java两个类 User.java如下: public class User{ privat ...
- Haoop Mapreduce 中的FileOutputFormat类
FileOutputFormat类继承OutputFormat,需要提供所有基于文件的OutputFormat实现的公共功能,主要有以下两点: (1)实现checkOutputSpecs方法 chec ...