cf445B 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个点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的更多相关文章
- CF 445B DZY Loves Chemistry(并查集)
题目链接: 传送门 DZY Loves Chemistry time limit per test:1 second memory limit per test:256 megabytes D ...
- 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 ...
- 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 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 ...
- 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 ...
- DZY Loves Chemistry
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)
题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂 总共有m对试剂能反应,按不同的 ...
- Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】
一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: #include < ...
随机推荐
- Wish | IT桔子
Wish | IT桔子 Wish www.wish.com 认领 关注 分享
- MVC 4.0语法 自动分页
4.0语法中实现自动分页只需要两个方法即可,Skip() ----跳过序列中指定的元素,Take()-----从序列的开头返回指定数量元素. 一般用自动分页都是无刷新的,可以把显示的数据,用局部页面封 ...
- JavaScript 定义类
ES6以前: function Point(x, y) { this.x = x; this.y = y; } Point.prototype.hello= function () { return ...
- google在线測试练习题1
Problem You receive a credit C at a local store and would like to buy two items. You first walk thro ...
- android键盘事件
在main.xml文件中代码如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- zend_db连接mysql(附完整代码)(转)
在看这些之前请确保你正确加载了PDO扩展. 作法是编辑php.ini手动增加下面这两行(前面要没有分号;):extension=php_pdo.dllextension=php_pdo_mysql.d ...
- H3 BPM 笔记
先通过流程设计器设计流程 注意 审批:1个人 会签: 多人用 同意时: 若为有一个同意就通过 则 审批选项卡 的同意出口 设为1 如果需要所有人同意才通过 则 审批选项卡 的同意出口 设为100% ...
- <display:column>属性解释
参考官方网站:http://www.displaytag.org/1.2/displaytag/tagreference.html 所有属性: autolink,class,comparator,de ...
- MVC路由规则以及前后台获取Action、Controller、ID名方法
1.前后台获取Action.Controller.ID名方法 前台页面:ViewContext.RouteData.Values["Action"].ToString(); Vie ...
- uva 498 - Polly the Polynomial
UVa 498: Polly the Polynomial | MathBlog #include <cstdio> #include <cstdlib> using name ...