zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    BZOJ 3611: [Heoi2014]大工程 [虚树 DP]
    BZOJ 3991: [SDOI2015]寻宝游戏 [虚树 树链的并 set]
    BZOJ 2286: [Sdoi2011消耗战 [DP 虚树]
    BZOJ 4767: 两双手 [DP 组合数]
    BZOJ 1426: 收集邮票 [DP 期望 平方]
    转「服务器运维」如何解决服务器I/O过高的问题
    iostat查看linux硬盘IO性能
    Linux前台、后台、挂起、退出、查看命令汇总
    Linux虚拟内存的作用
    -bash: iostat: command not found解决办法
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771523.html
Copyright © 2011-2022 走看看