• CodeForces 445B DZY Loves Chemistry


    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
    ",ans);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    ASP.NET控制cookie
    asp.net中Cookie的用法
    乱码问题
    JSP批量删除复选框选中记录
    JSP中根据时间查询
    关于<c:if>没有<c:else>解决方案
    Div
    Apache PIO 操作Excel
    将查询到的数据存到excel并下载
    做一个超链接,打开一个新窗口而保留原来的窗口
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771523.html
Copyright © 2020-2023  润新知