Codeforces Round #254 (Div. 2) 445B. DZY Loves Chemistry Solution
Problem Link: https://codeforces.com/problemset/problem/445/B
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- vector< long long> vec[100000];
- bool visited[100000];
- void dfs(int source) {
- visited[source] = 1;
- for (int i = 0; i < vec[source].size(); i++) {
- int next = vec[source][i];
- if (visited[next] == 0) dfs(next);
- }
- }
- int main() {
- long long ans,nodes, edges, cnt = 0;
- cin >> nodes >> edges;
- for (int i = 0; i < edges; i++) {
- long long u, v;
- cin >> u >> v;
- vec[u].push_back(v);
- vec[v].push_back(u);
- }
- for (int i = 1; i <= nodes; i++) {
- if (visited[i] == 0) {
- cnt++;
- dfs(i);
- }
- }
- ans=pow(2, nodes - cnt) ;
- cout << ans << endl;
- return 0;
- }
No comments