码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 445B DZY Loves Chemistry

时间:2015-08-30 21:05:49      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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

Description

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 Input

Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4

Hint

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

技术分享
 1 #include<stdio.h>
 2 
 3 int pre[1050];
 4 
 5 int Find(int x)          
 6 {
 7     int r=x;             
 8     while(r!=pre[r])     
 9         r=pre[r];        
10     int i=x,j;
11     while(pre[i]!=r)     
12     {
13         j=pre[i];
14         pre[i]=r;
15         i=j;
16     }
17     return r;            
18 }
19 
20 void join(int x,int y)               
21 {
22     int fx=Find(x),fy=Find(y);       
23     if(fx!=fy)                       
24         pre[fy]=fx;                  
25 }
26 
27 int main()
28 {
29     int n,m,i,j,x,y;
30     while(scanf("%d %d",&n,&m)!=EOF)
31     {
32         for(i=1;i<=n;i++)            
33             pre[i]=i;
34         for(i=1;i<=m;i++)            
35         {
36             scanf("%d %d",&x,&y);
37             join(x,y);
38         }
39         int t[1050]={0},k=0;       
40         for(i=1;i<=n;i++)            
41             t[Find(i)]=1;
42         for(i=1;i<=n;i++)
43             if(t[i]==1)
44                 k++;
45         long long ans=1;
46         for(i=1;i<=n-k;i++)
47             ans=ans*2;
48         printf("%I64d\n",ans);
49     }
50     return 0;
51 }
View Code

 

CodeForces 445B DZY Loves Chemistry

标签:

原文地址:http://www.cnblogs.com/cyd308/p/4771523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!