题目链接: 传送门

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. JavaEE PO VO BO DTO POJO DAO 整理总结

    佩服能将复杂难懂的技术,抽象成简单易懂事物的人. 厌恶将简单易懂的技术,添加一堆专业术语将别人弄的头晕目眩的人. PO VO BO DTO POJO DAO 总体一览: 1.DAO[data acce ...

  2. 【讲义提纲】以一个实战新闻cms增删改查demo为例,给学院国创队伍培训php

    PHP实战基础——以一个新闻cms的增删改查为例 一.        环境配置 二.        数据库创建 三.        增删改查demo 连接数据库 <?php $link=mysq ...

  3. 常用 redis 命令(for php)

    Redis 主要能存储 5 种数据结构,分别是 strings,hashes,lists,sets 以及 sorted sets. 新建一个 redis 数据库 $redis = new Redis( ...

  4. ajax请求过程中下载文件在火狐下的兼容问题

    项目中碰到的问题,记录如下. 需求很简单,点击一个文件链接下载该文件,同时向后台发送请求.需求很常见,用户点击下载后通常要进行下载量的统计,统计的话可以利用 script标签 或者 img标签(图片p ...

  5. .Net分布式异常报警系统-简介

    系统简介 分布式异常报警系统就是收集系统运行过程中产生的未处理异常,检查系统运行的状态,并将异常信息统一发送到服务端,由服务端将信息通知到相关的责任人.  问题 我们在项目开发中可能遇到以下几个问题: ...

  6. iostat命令详解

    iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devic ...

  7. 【Spring】构建Springboot项目 实现restful风格接口

    项目代码如下: package hello; import org.springframework.boot.SpringApplication; import org.springframework ...

  8. C# 多线程防止卡死

    软件界面的响应特性是判断一款软件的非常重要的方面.一般来说,不管你软件功能做得有多么奇妙,如果软件有一点点死机的感觉都会让用户感到很讨厌,甚至怀疑你软件里是否藏有更大的问题. 要提高界面的响应特性,最 ...

  9. HashTable、HashMap、HashSet

    1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...

  10. [转]Spring 注解总结

    原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...