CodeForces - 445B - DZY Loves Chemistry
2014-07-11 17:14
411 查看
先上题目:
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
445B
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
#include <bits/stdc++.h> #define LL long long #define MAX 52 using namespace std; int p[MAX]; int ss[MAX]; int findset(int x){ return p[x]==x ? x : p[x] = findset(p[x]);} void unionset(int y,int x){ y=findset(y); x=findset(x); p[y]=x; } void resetset(int n){ for(int i=0;i<=n;i++) p[i]=i; } int main() { int n,m,a,b; ios::sync_with_stdio(false); //freopen("data.txt","r",stdin); while(cin>>n>>m){ resetset(n); for(int i=0;i<m;i++){ cin>>a>>b; unionset(a,b); } memset(ss,0,sizeof(ss)); for(int i=1;i<=n;i++){ int o=findset(i); ss[o]++; } LL ans=1; for(int i=1;i<=n;i++){ if(ss[i]>0) ss[i]--; ans=ans<<ss[i]; } cout<<ans<<endl; } return 0; }
445B
相关文章推荐
- CodeForces 445B - DZY Loves Chemistry(并查集)
- 【codeforces 445B - DZY Loves Chemistry 】 + 并查集
- 【CodeForces】445B - DZY Loves Chemistry(并查集)
- CodeForces - 445B_DZY Loves Chemistry_并查集
- codeforces-B. DZY Loves Chemistry
- codeforces 445 B DZY Loves Chemistry【并查集】
- Codeforces 445B——并查集或DFS——DZY Loves Chemistry
- CF 445B(DZY Loves Chemistry-求连通块)
- [Codeforces] Round #254 (Div. 2) B. DZY Loves Chemistry
- 【Codeforces】-DZY Loves Chemistry(并查集变形)
- codeforces 445 B. DZY Loves Chemistry (并查集)
- E--DZY Loves Chemistry(CF-445B
- CF 445B(DZY Loves Chemistry-求连通块)
- Codeforces - B - DZY Loves Chemistry【水题】
- Codeforces 445 A DZY Loves Chessboard【DFS】
- codeforces 445E - DZY Loves Colors
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
- DZY Loves Chemistry
- Codeforces Round #254 (Div. 2):B. DZY Loves Chemistry
- CodeForces 444C. DZY Loves Physics(枚举+水题)