题意翻译
DZY热爱化学.他有n种物质,其中m对会反应.他把它们一种一种倒到烧杯里.有一个危险值,一开始等于1.如果一种物质倒到烧杯里之后至少有一种物质能和它反应,则危险值乘以二;否则危险值不变.求所有物质倒到烧杯里之后最大的危险值.
By @Fuko_Ibuki
题目描述
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n n n chemicals, and m m 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 1 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 2 2 . Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
输入输出格式
输入格式:
The first line contains two space-separated integers n n n and m m m .
Each of the next m m m lines contains two space-separated integers xi x_{i} xi and yi y_{i} yi (1<=xi<yi<=n) (1<=x_{i}<y_{i}<=n) (1<=xi<yi<=n) . These integers mean that the chemical xi x_{i} xi will react with the chemical yi y_{i} yi . Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 1 1 to n n n in some order.
输出格式:
Print a single integer — the maximum possible danger.
输入输出样例
输入样例#1: 复制
1 0
输出样例#1: 复制
1
输入样例#2: 复制
2 1 1 2
输出样例#2: 复制
2
输入样例#3: 复制
3 2 1 2 2 3
输出样例#3: 复制
4
说明
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 1 1 1 st chemical first, or pour the 2 2 2 nd chemical first, the answer is always 2 2 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).
用并查集维护即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 999999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }
int n, m;
ll ans;
int fa[maxn];
void init() {
for (int i = 0; i <= n; i++)fa[i] = i;
}
int findfa(int x) {
if(x == fa[x])return x;
return fa[x] = findfa(fa[x]);
}
void merge(int x, int y) {
if (findfa(x) != findfa(y)) {
fa[findfa(x)] = findfa(y);
}
}
int main()
{
//ios::sync_with_stdio(false);
rdint(n); rdint(m);
init(); ll ans = 1;
while (m--) {
int x, y;
rdint(x); rdint(y);
merge(x, y);
}
int tot = 0;
for (int i = 1; i <= n; i++) {
if (fa[i] == i)tot++;
}
cout << ((ll)1 << (n - tot)) << endl;
return 0;
}