B. DZY Loves Chemistry
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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

英文题真是伤不起啊

题意是说给定n个点m条双向边,一开始图是空的,用不同的顺序每次取一个点加入图中,如果图中有和它联通的点,那么ans*=2,最后求max(ans)

实际上对于图中的一个联通块,在联通块中无论加点的顺序如何,它对答案的贡献都是ans*=2^(个数-1)。当然图中有很多联通块,这随便乱搞一下就A了。反正我写的广搜

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
long long ans=1;
bool mark[1000];
int map[101][101];
inline long long bfs(int s)
{
if (mark[s]) return 1;
int q[10000]={0},t=0,w=1;
q[1]=s;mark[s]=1;
long long sigma=1;
while (t<w)
{
int now=q[++t];
for (int i=1;i<=n;i++)
if (!mark[i]&&map[now][i])
{
mark[i]=1;
sigma*=2;
q[++w]=i;
}
}
return sigma;
}
int main()
{
n=read();
m=read();
for (int i=1;i<=m;i++)
{
int x=read(),y=read();
map[x][y]=1;
map[y][x]=1;
}
cout<<endl;
for (int i=1;i<=n;i++)
{
ans*=bfs(i);
}
printf("%lld",ans);
}

cf445B DZY Loves Chemistry的更多相关文章

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

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

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

  3. CodeForces 445B DZY Loves Chemistry

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

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

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...

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

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

  7. DZY Loves Chemistry

    DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

    题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的 ...

  9. Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】

    一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: #include < ...

随机推荐

  1. hadoop2.2.0的ha分布式集群搭建

    hadoop2.2.0 ha集群搭建 使用的文件如下:    jdk-6u45-linux-x64.bin    hadoop-2.2.0.x86_64.tar    zookeeper-3.4.5. ...

  2. 为什么p标签不能嵌套div??

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  3. Css定位-定位

    在CSS中一共有N种定位方式,其中,static ,relative,absolute三种方式是最基本最常用的三种定位方式.他们的基 本介绍如下. static默认定位方式 relative相对定位, ...

  4. wpf下拉框不能多选的原因

    <dxe:ComboBoxEdit Margin="0"  Height="25" Width="65" VerticalAlignm ...

  5. IOS设计模式学习(19)策略

    1 前言 面向对象软件设计中,我们可以把相关算法分离为不同的类,成为策略.与这种做法有关的一种设计模式成为策略模式. 2 详述 2.1 简述 策略模式中得一个关键角色是策略类,它为所有支持的或相关的算 ...

  6. 将16进制颜色转换成UIColor-ios

    -(UIColor *) hexStringToColor: (NSString *) stringToConvert { NSString *cString = [[stringToConvert ...

  7. Linux命令之用户与组管理

    介绍 Linux操作系统中,任何文件都归属某一特定的用户,而任何用户都隶属至少一个用户组.用户是否有权限对某文件进行访问.读写以及执行,受到系统严格约束的正式这种清晰.严谨的用户与用户组管理系统.在很 ...

  8. Win10开发必备工具:Visual Studio 2015正式版下载

    7月21日凌晨最新消息,面向大众用户的Visual Studio 2015集成开发工具正式版免费试用版已经推出.本文帮大家汇总一下简体中文社区版.专业版以及企业版在线安装版以及ISO离线安装镜像下载地 ...

  9. QQ 国际版(International version) - 关闭弹出资讯

    1,打开QQ面板,点击左下角的 "企鹅"图标.选择 "Setting". 2,在弹出的 "Setting"面板中,选择 "Priv ...

  10. C#类中字段,属性与方法

    person类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...