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 11 2

Output

2

Input

3 21 22 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).

大意:

就是说,有那么一个人,他喜欢化学,喜欢把化学药品掺起来。

现在呢他有n种化学药品,其中的m种会起反应,他想要把这些药品都放到一个试管里,还得一个一个倒

??然后问题就来了,要我们考虑下试管的危险性(跟我们什么有关系????)空试管的危险等级为一级,然后这个人会往试管里加一种药品,如果试管里已经有一个或多个药品能和

这种药品反应,那么试管的危险等级将乘以二,否则等级不变。让我们找出按照最佳药品倾倒顺序所产生的最大危险性。

分析:

按照输入来讲,n种药品,按1~n的顺序编号,m对药品会反应,然后还说,在输入的时候每对药品只要输入一遍(这会让我少想一些东西)想一下,发现可以先查找集合,先将能与多个药品产生反应的药品倒入,想想,如果n种药品都反应,则能完全反应的反应次数为(n-1)(第一个放入未反应),则能完全反应的危险数为为 2^(n-1),则除第一个子集外,遇到一个子集危险数就除以2,最后结果就为 2^(n-子集数)。需要注意的是,最后的安全等级可能会很大,int会爆掉。想明白就很简单的一道题。

附代码:

 1 #include<map>
 2 #include<cmath>
 3 #include<queue>
 4 #include<vector>
 5 #include<cstdio>
 6 #include<cstring>
 7 #include<iostream>
 8 #include<algorithm>
 9 using namespace std;
10 int p[100005];
11 int find(int x)
12 {
13     return p[x]==x?x:p[x]=find(p[x]);
14 }
15 void intt()
16 {
17     for(int i=0; i<100005; i++)
18         p[i]=i;
19 }
20 void nion(int a,int b)
21 {
22     int f1=find(a);
23     int f2=find(b);
24     if(f1!=f2)
25         p[f2]=f1;
26 }
27 int main()
28 {
29     int n,m;
30     while(~scanf("%d%d",&n,&m))
31     {
32         intt();
33         while(m--)
34         {
35             int a,b;
36             scanf("%d%d",&a,&b);
37             nion(a,b);
38         }
39         long long  sum=0;//注意大小
40         for(int i=1; i<=n; i++)
41             if(p[i]==i)
42                 sum++;
43         sum=pow(2,n-sum);
44         printf("%lld\n",sum);
45     }
46     return 0;
47 }

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).

原文地址:https://www.cnblogs.com/She-Chuan/p/9497551.html

时间: 2024-10-10 18:34:57

DZY Loves Chemistry (并查集)的相关文章

Codeforces 445B DZY Loves Chemistry(并查集)

题目链接:Codeforces 445B DZY Loves Chemistry 题目大意:有若干种化学药品,给出两两会反应的关系,现在要将药物依次放入一个容器中,容器中的化学药品可以互相反应,如果当前放入的药品能与已经在容器中的某一药品反应,那么危险值翻倍,即*2,初始值为1,求一顺序,使得为危险值最大. 解题思路:并查集求最小联通分量s,2n?s即为答案. #include <cstdio> #include <cstring> #include <iostream>

BZOJ 3563 DZY Loves Chinese 并查集

题目大意:给定一个无向联通图,q次询问当图中某k条边消失时图是否联通 强制在线 逗比题233 不明白什么意思的去看DZY Loves Chinese II的红字就明白这题为何逗比了0.0 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 100100 using namespace std; struct edges{ int x,y

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

题面:http://uoj.ac/problem/14 考虑只有前两个操作怎么做. 每次删除一定是从后往前删,并且被删的边如果不是树边则没有影响,如果是树边也不存在边能替代. 直接删除这条边就可以. 于是用一个栈来保存现场,然后按秩合并的并查集维护就OK了. 现在有撤回操作,但根据上面对删边分析出的性质. 可以这样: 如果是插入一条边,然后撤回,相当于删边. 如果删边然后撤回,相当于什么也不做. 代码还是很好理解的. 代码: #include <cstdio> #include <cst

Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)

题目链接 昨天晚上没有做出来,刚看题目的时候还把题意理解错了,当时想着以什么样的顺序倒,想着就饶进去了, 也被题目下面的示例分析给误导了. 题意: 有1-n种化学药剂  总共有m对试剂能反应,按不同的次序将1-n种试剂滴入试管,如果正在滴入的试剂能与已经滴入 的试剂反应,那么危险数*2,否则维持不变.问最后最大的危险系数是多少. 分析:其实这个题根本不用考虑倒入的顺序,只是分块就行,结果就是每个子集里元素的个数-1 和  的2的幂. 1 #include <iostream> 2 #inclu

Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】

一开始不知道题意是啥意思,迟放进去反应和后放进去反应有什么区别 对于第三组数据不是很懂,为啥312,132的组合是不行的 后来发现这是一道考察并查集的题目 QAQ 怒贴代码: 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <iostream> 6 #include <algorithm> 7

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

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

CodeForces 445B DZY Loves Chemistry

DZY Loves Chemistry Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 445B Description DZY loves chemistry, and he enjoys mixing chemicals. DZY has n chemicals, and m pairs of them will re

codeforces 445 B DZY Loves Chemistry【并查集】

题意:给出n种化学物质,其中m对会发生化学反应,每次加入化学物质进去的时候, 如果有能够和它发生反应的,危险值就乘以2,问怎样的放入顺序使得危险值最大 将这m对会反应的用并查集处理,统计每个连通块里面的元素个数,再将其减去1,加起来,就是2的指数 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #i

HDU-4879-ZCC loves march(map+set+并查集)

Description On a m*m land stationed n troops, numbered from 1 to n. The i-th troop's position can be described by two numbers (xi,yi) (1<=xi<=m,1<=yi<=m). It is possible that more than one troop stationed in the same place. Then there are t mi