The topics are as follows:
Time limit:1000ms Memory limit:262144kb 64bit IO format:%i64d &%i64u
Submit
Status
Practice
Codeforces 445B
Description
Dzy loves chemistry, and he enjoys mixing chemicals.
Dzy has n chemicals, and m pairs of them would 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 is already one or more chemicals in the test tube so can react with It, the danger of the test tube is 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 the space-separated integers n and M.
Each of the next m lines contains, space-separated integers xi and yi (1≤xi < yi≤n). These integers mean that the chemical Xi would react with the chemical Yi. Each pair of chemicals would 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 Input
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Hint
In the first sample, there's only one-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 are always 2.
In the third sample, there was four ways to achieve the maximum possible danger:2-1-3, 2-3-1, and 3-2-1 (which is th E numbers of the chemicals in order of pouring).
Find out the most dangerous situation, also only need to use and find the corresponding Unicom block;
The code is as follows:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int pre[55];
int find_root (int x) {return pre[x]==x?x:pre[x]=find_root (pre[x]);}
int sum[55];
int maxn=-1;
int n,m;
int main ()
{
cin>>n>>m;
if (n==1)
{
cout<<1<<endl;
return 0;
}
memset (pre,0,sizeof (pre));
for (int i=1;i<=n;i++)
{
pre[i]=i;
sum[i]=1;
}
int rx1,ry1;
for (int i=0;i<m;i++)
{
int x1,y1;
cin>>x1>>y1;
Rx1=find_root (x1);
Ry1=find_root (y1);
if (rx1!=ry1)
{
pre[rx1]=ry1;
}
}
Long long mul=1;
for (int i=1;i<=n;i++)
{
if (pre[i]!=i)
{
mul*=2;
}
}
printf ("%i64d\n", mul);
return 0;
}
Only on behalf of personal views, welcome to Exchange discussion, do not spray ~ ~ ~
Photoby:wlop
Http://weibo.com/wlop