博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces 445B DZY Loves Chemistry
阅读量:5995 次
发布时间:2019-06-20

本文共 2759 字,大约阅读时间需要 9 分钟。

DZY Loves Chemistry
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit     

Description

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 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 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 #include
2 3 int pre[1050]; 4 5 int Find(int x) 6 { 7 int r=x; 8 while(r!=pre[r]) 9 r=pre[r]; 10 int i=x,j;11 while(pre[i]!=r) 12 {13 j=pre[i];14 pre[i]=r;15 i=j;16 }17 return r; 18 }19 20 void join(int x,int y) 21 {22 int fx=Find(x),fy=Find(y); 23 if(fx!=fy) 24 pre[fy]=fx; 25 }26 27 int main()28 {29 int n,m,i,j,x,y;30 while(scanf("%d %d",&n,&m)!=EOF)31 {32 for(i=1;i<=n;i++) 33 pre[i]=i;34 for(i=1;i<=m;i++) 35 {36 scanf("%d %d",&x,&y);37 join(x,y);38 }39 int t[1050]={
0},k=0; 40 for(i=1;i<=n;i++) 41 t[Find(i)]=1;42 for(i=1;i<=n;i++)43 if(t[i]==1)44 k++;45 long long ans=1;46 for(i=1;i<=n-k;i++)47 ans=ans*2;48 printf("%I64d\n",ans);49 }50 return 0;51 }
View Code

 

转载于:https://www.cnblogs.com/cyd308/p/4771523.html

你可能感兴趣的文章
C#提高知识 ADO.NET实体数据模型(1)
查看>>
送给那些搞电脑维修的人儿
查看>>
Exchange Server 2013预览版服务器角色概况
查看>>
IPSec ***和SSL ***两种***的安全风险比较
查看>>
Hadoop入门扫盲:hadoop发行版介绍与选择
查看>>
实战1:创建Windows Server 2008域
查看>>
在 Windows 2012 R2 安装 SharePoint 2013
查看>>
《统一沟通-微软-实战》-6-部署-5-边缘服务器-2012-07-12-3
查看>>
活动目录域控制器最长可以离线多久?
查看>>
红帽转型为云计算解决方案提供商
查看>>
疯狂ios之cocos2d中的文本
查看>>
Mac下通过 brew 安装不同版本的php
查看>>
云在天之南——我的七天七夜(率性苍山洱海)
查看>>
如何迅速入门Shell 编程
查看>>
Linux企业应用微博客正式开通
查看>>
64位linux下的gns3网络模拟器配置
查看>>
效果差学费贵售后难,VIPKID米雯娟的野心不能只靠“烧钱”营销
查看>>
Windows Server 2012 R2 WSUS-10:流程概述
查看>>
自动发现服务是怎样工作的?
查看>>
Office 365 系列之七:安装Office 365 ProPlus
查看>>