Codeforces Round #254(Div.2)SDUT 2015 여름방학 훈련 14급 주간 경기 2(및 조회)
2292 단어 함께 조사하여 모으다
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
물 모으기 문제를 찾았지만 경기 때 풀지 못했다--!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int init[100010],t[100010],p[100010];
int s=0;
int fi(int r)
{
return r==init[r]?r:init[r]=fi(init[r]);
}
void mer(int x,int y)
{
int a=fi(x);
int b=fi(y);
if(a>b)
init[a]=b;
else
init[b]=a;
}
int main()
{
int n,m,i,j,k,x,y;
while(scanf( "%d%d",&n,&m)!=EOF )
{
s=0;
for(i=1;i<=n;i++)
init[i]=i;
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
mer(x,y);
}
for(i=1;i<=n;i++)// , s++, 。。
{
if(init[i]!=i)
{
s++;
}
}
s=n-s;
printf("%.0lf
",pow(2,s));
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
POJ 2236 Wireless Network 간편한 검색 및 수집The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftersho...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.