DZY Loves Chemistry (并查集)

Posted she-chuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DZY Loves Chemistry (并查集)相关的知识,希望对你有一定的参考价值。

题目:

(??,最近净做并查集了还一道难的都不会)

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.

Examples
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种会起反应,他想要把这些药品都放到一个试管里,还得一个一个倒

??然后问题就来了,要我们考虑下试管的危险性(跟我们什么有关系????)空试管的危险等级为一级,然后这个人会往试管里加一种药品,如果试管里已经有一个或多个药品能和

这种药品反应,那么试管的危险等级将乘以二,否则等级不变。让我们找出按照最佳药品倾倒顺序所产生的最大危险性。

分析:

按照输入来讲,n种药品,按1~n的顺序编号,m对药品会反应,然后还说,在输入的时候每对药品只要输入一遍(这会让我少想一些东西)想一下,发现可以先查找集合,先将能与多个药品产生反应的药品倒入,想想,如果n种药品都反应,则能完全反应的反应次数为(n-1)(第一个放入未反应),则能完全反应的危险数为为 2^(n-1),则除第一个子集外,遇到一个子集危险数就除以2,最后结果就为 2^(n-子集数)。需要注意的是,最后的安全等级可能会很大,int会爆掉。想明白就很简单的一道题。

附代码:

 

 1 #include<map>
 2 #include<cmath>
 3 #include<queue>
 4 #include<vector>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 int p[100005];
11 int find(int x)
12 {
13     return p[x]==x?x:p[x]=find(p[x]);
14 }
15 void intt()
16 {
17     for(int i=0; i<100005; i++)
18         p[i]=i;
19 }
20 void nion(int a,int b)
21 {
22     int f1=find(a);
23     int f2=find(b);
24     if(f1!=f2)
25         p[f2]=f1;
26 }
27 int main()
28 {
29     int n,m;
30     while(~scanf("%d%d",&n,&m))
31     {
32         intt();
33         while(m--)
34         {
35             int a,b;
36             scanf("%d%d",&a,&b);
37             nion(a,b);
38         }
39         long long  sum=0;//注意大小
40         for(int i=1; i<=n; i++)
41             if(p[i]==i)
42                 sum++;
43         sum=pow(2,n-sum);
44         printf("%lld
",sum);
45     }
46     return 0;
47 }

 

 

 

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
























































以上是关于DZY Loves Chemistry (并查集)的主要内容,如果未能解决你的问题,请参考以下文章

UOJ_14_UER #1DZY Loves Graph_并查集

CodeForces - 445B - DZY Loves Chemistry-转化问题

bzoj 3569: DZY Loves Chinese II

[CF444E]DZY Loves Planting

BZOJ3563 DZY Loves Chinese

HDU 4879 ZCC loves march (并查集,set,map)