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



   Problem Link:  https://codeforces.com/problemset/problem/445/B

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. vector< long long> vec[100000];
  10. bool visited[100000];
  11. void dfs(int source) {
  12. visited[source] = 1;
  13. for (int i = 0; i < vec[source].size(); i++) {
  14. int next = vec[source][i];
  15. if (visited[next] == 0) dfs(next);
  16. }
  17. }
  18. int main() {
  19. long long ans,nodes, edges, cnt = 0;
  20. cin >> nodes >> edges;
  21. for (int i = 0; i < edges; i++) {
  22. long long u, v;
  23. cin >> u >> v;
  24. vec[u].push_back(v);
  25. vec[v].push_back(u);
  26. }
  27. for (int i = 1; i <= nodes; i++) {
  28. if (visited[i] == 0) {
  29. cnt++;
  30. dfs(i);
  31. }
  32. }
  33. ans=pow(2, nodes - cnt) ;
  34. cout << ans << endl;
  35. return 0;
  36. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.