Codeforces - B - DZY Loves Chemistry【水题】

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.

Sample test(s)
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).

题目大意

有1-n种化学药剂  总共有m对试剂能反应,按不同的次序将1-n种试剂滴入试管,如果正在滴入的试剂能与已经滴入的试剂反应,那么危险数*2,否则维持不变。

问最后最大的危险系数是多少

题目解析

并查集的应用,能完全反应的反应次数为(n-1)(第一个放入未反应),则能完全反应的危险数为为 2^(n-1),则除第一个子集外,遇到一个子集危险数就除以2,最后结果就为 2^(n-子集数)

# include<stdio.h>
# include<string.h>

/*
本想用全排列,首先会超时,其次更加麻烦,易出错 
int G[55][55], max, num, sum, A[55];

void permutation(int n, int *A, int cur)
{
	int i, j;
	if(cur == n)
	{
		num = 1;
		for(i = 0; i < n; i++)	//第i个试管 
		//	printf("%d ",A[i]);
//		printf("\n");
			for(j = 0; j < i; j++)	//i之前个试管 
			{
				if(G[A[i]][A[j]]==1)
				{
					num++;
					break;
				}	
			}
		if(num > max)
		{
			max = num;
			sum = 1;	
		} 
		else if(num == max)
		{
			sum++;
		}
	}
	else
	{
		for(i = 1; i <= n; i++)
		{
			int ok = 1;
			for(j = 0; j < cur; j++)
			{
				if(A[j] == i)
					ok = 0;
			}
			if(ok)
			{
				A[cur] = i;
				permutation(n, A, cur+1);
			}
		}
	}
}


int main()
{
	int n, m, x, y;
	while(~scanf("%d%d",&n,&m))
	{
		while(m--)
		{
			scanf("%d%d",&x,&y);
			G[x][y] = G[y][x] = 1;
		}
		for()
		max = num = 1;
		sum = 0;
		permutation(n, A, 0);
		printf("%d\n",sum); 
	}
	
	return 0;
}
*/
int father[110];

int find(int x)
{
	if(father[x]!=x)
		father[x] = find(father[x]);
	return father[x];
}
int main()
{
	int n, m, x, y;
	while(scanf("%d%d", &n, &m) != EOF)
	{
		for(int i = 1; i <= n; i++)
			father[i] = i;
		while(m--)
		{
			scanf("%d%d", &x,&y);
			int a = find(x);
			int b = find(y);
			father[a] = b; 
		} 
		long long ans = 1LL << n;
		for(int i = 1; i <= n; i++)
		{
			if(father[i] == i)
				ans /= 2;
		}
		printf("%lld\n",ans);//注意输出格式 
	} 
	
	return 0;
}



你可能感兴趣的:(Codeforces - B - DZY Loves Chemistry【水题】)