1. 程式人生 > >DZY Loves Chemistry (並查集)

DZY Loves Chemistry (並查集)

max queue other pear contains The orm code 個人

題目:

(??,最近凈做並查集了還一道難的都不會)

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 y

i. 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\n",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 (並查集)