Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry

來源:互聯網
上載者:User

Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry

B. DZY Loves Chemistrytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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 11 2
output
2
input
3 21 22 3
output
4
思路:根據反應關係找到所有的集合,再在集合中求得結果(因為n<=50,所以用__int64)

代碼:

#include <stdio.h>int father[55];int find(int x){if (father[x] == x)return x;elsereturn (father[x] = find(father[x]));}void merge(int a, int b){int x, y;x = find(a);y = find(b);if (x != y)father[x] = y;}int main(){int n, m;while (scanf("%d%d", &n, &m) != EOF){for (int i = 1; i <= n; i++)father[i] = i;while (m--){int x, y;scanf("%d%d", &x, &y);merge(x, y);}__int64 ans = 1;for (int i = 1; i <= n; i++){int fa = father[i];if (fa == i){int s = 2;for (int j = 1; j <= n; j++)if (find(j) == fa&&i != j){ans = ans*s;}}}printf("%I64d\n", ans);}return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.