Codeforces Round #254 (Div. 2) 445B - DZY Loves Chemistry (并查集)
发布日期:2021-11-06 16:56:36 浏览次数:4 分类:技术文章

本文共 1959 字,大约阅读时间需要 6 分钟。

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 11 2
output
2
input
3 21 22 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).

AC代码:

#include 
//author:XXYYint fa[55];int find(int x){ if(fa[x]!=x) fa[x]=find(fa[x]); return fa[x];}void uni(int x,int y){ x=find(x); y=find(y); if(x==y) return; fa[x]=y;}int main(){ int n,m,i,x,y,j,a,b; __int64 sum; while(~scanf("%d%d",&n,&m)){ if(m==0) printf("1\n"); else{ for(i=1;i<=n;i++) fa[i]=i; for(i=0;i

转载地址:https://blog.csdn.net/YJX_xx/article/details/37512039 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Codeforces Round #254 (Div. 2)444A - DZY Loves Physics (枚举权值求最大)
下一篇:Codeforces Round #254 (Div. 2)445A DZY Loves Chessboard(字符串处理)

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月11日 16时53分10秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章