Codeforces445B(SummerTrainingDay06-N 并查集)
B. DZY Loves Chemistry
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.
Examples
input
1 0
output
1
input
2 1
1 2
output
2
input
3 2
1 2
2 3
output
4
Note
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).
能够相互反应的元素构成一个集合,用并查集找到有几个集合,从每个集合中各取一个放入杯中,然后不管从哪个集合取一个元素加入杯中都能够有办法翻倍,所以答案就是2的(元素个数减去集合个数)次方。
//2017-08-06
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int fa[N], arr[N];
long long pow[N]; void init(){
pow[] = ;
for(int i = ; i < N; i++){
fa[i] = i;
pow[i] = pow[i-]*;
}
} int getfa(int x){
if(fa[x] == x)return x;
return fa[x] = getfa(fa[x]);
} void Merge(int a, int b){
int af = getfa(a);
int bf = getfa(b);
if(af != bf){
fa[bf] = af;
}
} int main()
{
int n, m, a, b;
while(scanf("%d%d", &n, &m)!=EOF){
init();
while(m--){
scanf("%d%d", &a, &b);
Merge(a, b);
}
int cnt = ;
for(int i = ; i <= n; i++)
if(fa[i] == i)cnt++;
if(m == )printf("1\n");
else printf("%lld\n", pow[n-cnt]);
} return ;
}
import java.util.*;
public class Main{
static final int N = 60;
static int[] fa = new int[N];
static int[] arr = new int[N];
static long[] pow = new long[N];
static void init(){
pow[0] = 1;
for(int i = 1; i < N; i++){
fa[i] = i;
pow[i] = pow[i-1]*2;
}
}
static int getfa(int x){
if(fa[x] == x)return x;
return fa[x] = getfa(fa[x]);
}
static void merge(int a, int b){
int af = getfa(a);
int bf = getfa(b);
if(af != bf)
fa[bf] = af;
}
public static void main(String args[]){
int n, m, a, b;
Scanner cin = new Scanner(System.in);
while(cin.hasNext()){
n = cin.nextInt();
m = cin.nextInt();
init();
for(int i = 0; i < m; i++){
a = cin.nextInt();
b = cin.nextInt();
merge(a, b);
}
int cnt = 0;
for(int i = 1; i <= n; i++)
if(fa[i] == i)
cnt++;
if(m == 0)System.out.printf("1\n");
else System.out.println(pow[n-cnt]);
}
}
}
Codeforces445B(SummerTrainingDay06-N 并查集)的更多相关文章
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
- 关押罪犯 and 食物链(并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- bzoj1854--并查集
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Codeforces 731C Socks 并查集
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...
随机推荐
- 使用Flask+uwsgi+Nginx部署Flask正式环境
环境准备 在开始正式讲解之前,我们将首先进行环境准备. Step1:安装Python,pip以及nginx: sudo apt-get update sudo apt-get install pyth ...
- Spring学习笔记1——IOC: 尽量使用注解以及java代码
在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...
- grub 引导修复
- 冰与火之歌居然是在 DOS 系统上写出来的
简评:<权力的游戏>第八季(最终季)终于开播了!这部美剧的原著小说有一个很有趣的冷知识 -- 它是在运行 DOS 系统的计算机上写出来的.其实不少老粉都已经知道这个典故,不过听到老爷子的亲 ...
- iOS开发之Todo List for Swift项目
一直从事Windows Phone开发,但对iOS开发一直有所好奇,于是在MBP到手之际,顺手安装了Xcode.移动互联网开发的相似性,使得我能快速地了解和认识了iOS的开发框架体系,在看完了Appl ...
- centos7 python2和python3共存
一.解决Python2 pip问题 在centos7中安装好操作系统,自带的是Python2的版本,但是并没有pip的方法,我们需要自行安装 报名为python-pip # 默认python2的版本 ...
- Scala的Trait详解
http://article.yeeyan.org/view/178378/358355
- python使用(三)
1.function_option.py2.function_code_option.py3.thread_option.py4.class_option.py5.threading_option.p ...
- redmine设置user projects时无法delete的处理方法
对于user,当要在管理员界面处理其projects权限时,发现部分项目只有edit按钮,而部分项目还有一个delete按钮. “delete”,直接点击按钮即可删除对应project权限,表明该pr ...
- Android使用bindService作为中间人对象开启服务
Android使用bindService作为中间人对象开启服务 项目结构如下: MyService: package com.demo.secondservice; import android.ap ...