您的位置:首页 > 其它

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

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: