【codeforces 445B - DZY Loves Chemistry 】 + 并查集
来源:互联网 发布:算法英文缩写 编辑:程序博客网 时间:2024/04/27 20:16
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代码 :
#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 445B DZY Loves Chemistry (并查集)
- CodeForces 445B - DZY Loves Chemistry(并查集)
- 【codeforces 445B - DZY Loves Chemistry 】 + 并查集
- CodeForces 445B DZY Loves Chemistry (并查集)
- codeforces 445 B. DZY Loves Chemistry (并查集)
- 【CodeForces】445B - DZY Loves Chemistry(并查集)
- Codeforces 455B DZY Loves Chemistry【并查集】
- Codeforces Round #254 (Div. 2) 445B - DZY Loves Chemistry (并查集)
- codeforces 445B DZY Loves Chemistry 贪心 dfs 并查集
- 【Codeforces】-DZY Loves Chemistry(并查集变形)
- Codeforces 445B. DZY Loves Chemistry
- Codeforces 445B DZY Loves Chemistry
- CODEFORCES 445B DZY Loves Chemistry
- Codeforces 445B DZY Loves Chemistry
- CodeForces 445B DZY Loves Chemistry
- JSP第一天
- 重构资料
- 最全面的常用正则表达式大全
- 浅谈java中的多线程
- 机器学习算法-决策树
- 【codeforces 445B - DZY Loves Chemistry 】 + 并查集
- 二叉链表的存储结构和基本操作(各种遍历、求树深度、求树叶个数)
- HTML总结
- session的存储方式和配置
- Win 7下缷载Matlab 7.1出现“exeption calling main”错误的解决办法
- 创建数据库笔记(一)
- 120. Triangle
- Spring security 用户,角色,权限,资源
- 15Q游戏(1)27(2)43(3)