【codeforces 445B - DZY Loves Chemistry 】 + 并查集
2016-10-27 00:13
861 查看
B. DZY Loves Chemistry
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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).
并查集 + 路径压缩 ~~~最后判断下,只要根节点不是他本身~~就翻倍~~~(刚开始,居然想找个最多的根节点数,ZSZZ)
AC代码 :
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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).
并查集 + 路径压缩 ~~~最后判断下,只要根节点不是他本身~~就翻倍~~~(刚开始,居然想找个最多的根节点数,ZSZZ)
AC代码 :
#include<bits/stdc++.h> using namespace std; typedef long long LL; int vis[55],in[55]; LL KSM(int x){ LL cut = 2,ans = 1; while(x){ if(x & 1) ans *= cut; cut *= cut; x >>= 1; } return ans; } int find(int i){ int j = i , t; while(i != vis[i]) i = vis[i]; while(j != i){ t = vis[j]; vis[j] = i; j = t; } return i; } void add(int x,int y){ int fx = find(x); int fy = find(y); if(fx != fy) vis[fy] = fx; } int main() { int n,m,a,b,cut = 0; scanf("%d %d",&n,&m); for(int i = 1 ; i <= n ; i++) vis[i] = i,in[i] = 0; while(m--){ scanf("%d %d",&a,&b); add(a,b); } for(int i = 1 ; i <= n ; i++) if(vis[i] == i) cut++; int ans = n - cut; printf("%lld\n",KSM(ans)); return 0; }
相关文章推荐
- CodeForces - 445B_DZY Loves Chemistry_并查集
- CodeForces 445B - DZY Loves Chemistry(并查集)
- 【CodeForces】445B - DZY Loves Chemistry(并查集)
- codeforces 445 B DZY Loves Chemistry【并查集】
- codeforces 445 B. DZY Loves Chemistry (并查集)
- 【Codeforces】-DZY Loves Chemistry(并查集变形)
- CodeForces - 445B - DZY Loves Chemistry
- Codeforces Round #254 (Div. 2) B. DZY Loves Chemistry (并查集)
- Codeforces Round #254 (Div. 2) DZY Loves Chemistry【并查集基础】
- codeforces-B. DZY Loves Chemistry
- Codeforces Round #254 (Div. 2)B DZY Loves Chemistry(并查集)
- Codeforces Round #254 (Div. 2)B DZY Loves Chemistry(并查集)
- BZOJ 3520: Dzy loves chemistry 1【并查集,卡精度
- Codeforces Round #254 (Div. 2) 445B - DZY Loves Chemistry (并查集)
- CF445B:DZY Loves Chemistry(并查集)
- E--DZY Loves Chemistry(CF-445B
- [Codeforces] Round #254 (Div. 2) B. DZY Loves Chemistry
- CF 445B(DZY Loves Chemistry-求连通块)
- CF 445B(DZY Loves Chemistry-求连通块)
- Codeforces 445B——并查集或DFS——DZY Loves Chemistry