题目链接: 传送门

DZY Loves Chemistry

time limit per test:1 second     memory limit per test:256 megabytes

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

1 0

2 1
1 2

3 2
1 2
2 3

Sample Output

1

2

4

思路:

题目大意:给你n种化学物品,其中有的化学物品能反应,若加入一种化学物品能与试管中已有的物品反应,则危险值×2,问最大的危险值。
最大危险值很明显是2^(n-v),其中“v”为连通块的个数。比如1-6六个数,1-3、4-5、6分为三个连通块,显然只有在同一个连通块里的化学物品能反应。

#include <cstdio>
using namespace std;

int n, m, fa[100], x, y;

int gf(int x)
{
    if (fa[x] != x) fa[x] = gf(fa[x]);
    return fa[x];
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)    fa[i] = i;
    while (m--)
    {
        scanf("%d%d", &x, &y);
        fa[gf(x)] = gf(y);
    }
    long long ans = (1LL << n);
    for (int i = 1; i <= n; i++)
        if (gf(i) == i) ans /= 2;
    printf("%I64d\n", ans);
}
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef __int64 LL;
int ans[55];
int N,M;
void init()
{
    for (int i = 1;i <= N;i++)
    {
        ans[i] = i;
    }
}

int main()
{
    while (~scanf("%d%d",&N,&M))
    {
        int u,v;
        LL res = 1;
        memset(ans,0,sizeof(ans));
        init();
        while (M--)
        {
            scanf("%d%d",&u,&v);
            while (u != ans[u])
            {
                u = ans[u];
            }
            while (v != ans[v])
            {
                v = ans[v];
            }
            if (u != v)
            {
                res *= 2;
            }
            ans[v] = u;
        }
        printf("%I64d\n",res);
    }
    return 0;
}

CF 445B DZY Loves Chemistry(并查集)的更多相关文章

  1. UOJ_14_【UER #1】DZY Loves Graph_并查集

    UOJ_14_[UER #1]DZY Loves Graph_并查集 题面:http://uoj.ac/problem/14 考虑只有前两个操作怎么做. 每次删除一定是从后往前删,并且被删的边如果不是 ...

  2. CodeForces 445B. DZY Loves Chemistry(并查集)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...

  3. CodeForces 445B DZY Loves Chemistry

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

  4. CF 445B(DZY Loves Chemistry-求连通块)

    B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. CodeForces 445B DZY Loves Chemistry (并查集)

    题意: 有N种药剂编号 1 ~ N,然后有M种反应关系,这里有一个试管,开始时危险系数为 1,每当放入的药剂和瓶子里面的药剂发生反应时危险系数会乘以2,否则就不变,给出N个药剂和M种反应关系,求最大的 ...

  6. cf444E. DZY Loves Planting(并查集)

    题意 题目链接 Sol 神仙题啊Orzzzzzz 考场上的时候直接把树扔了对着式子想,想1h都没得到啥有用的结论. 然后cf正解居然是网络流??出给NOIP模拟赛T1???¥%--&((--% ...

  7. UOJ14 DZY Loves Graph 并查集

    传送门 题意:给出一张$N$个点,最开始没有边的图,$M$次操作,操作为加入边(边权为当前的操作编号).删除前$K$大边.撤销前一次操作,每一次操作后询问最小生成树边权和.$N \leq 3 \tim ...

  8. codeforces 445B. DZY Loves Chemistry 解题报告

    题目链接:http://codeforces.com/problemset/problem/445/B 题目意思:给出 n 种chemicals,当中有 m 对可以发生反应.我们用danger来评估这 ...

  9. DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏

    DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. C# 7.0 新特性4: 返回引用

    本文参考Roslyn项目中的Issue:#118. 1. C# 7.0 新特性1: 基于Tuple的“多”返回值方法 2. C# 7.0 新特性2: 本地方法 3. C# 7.0 新特性3: 模式匹配 ...

  2. idea 生成代码中带参数final修饰

  3. 记、基于react-router的单页应用

    现在用react写单页应用基本上都是用react-router做前端路由了吧!最近在使用react-router的过程中遇到了不少问题,在这里总结一下. 浏览器url react-router默认提供 ...

  4. Linux vmstat命令实战详解

    vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...

  5. C#实现清理系统内存

    金山内存整理工具.360内存清理工具非常好用,可以将系统内存最小化,提升系统运行速度.其实这些事情C#也可以做到,原理就是对系统进程中的进程内存进行逐个优化. 网上大多推荐使用系统的SetProces ...

  6. 贪吃蛇C#和JAVA实现

    using System; using System.Windows.Forms; using System.Drawing; class Window : Form { Point[] a = ]; ...

  7. LVS+Redis部署手册

    Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...

  8. linux下PHP7环境搭建

    LAMP环境版本 操作系统:Centos 7 Mysql:5.7.11 Apache:2.4.18 PHP:7.0.4     安装Mysql   下载链接:http://dev.mysql.com/ ...

  9. Day Four(Beta)

    站立式会议 站立式会议内容总结 331 今天:增加了若干话题:建立chat–user类,查阅bmob文档. 明天: 学习bmob的sql操作 442 今天:登录信息在主页上更新数据 遇到的问题:无 明 ...

  10. Beta版本冲刺Day4

    会议讨论: 628:由于昨天的考试我们组目前只把项目放到了服务器上,配置Java环境遇到了问题.601:将一些原来的界面进行了修改,修改成了更加美观的外形. 528:进行一些还未完成得到功能,比如查询 ...