Codeforces Round #170 (Div. 1) 277A. Learning Languages Solution


 

Problem  Link  :  https://codeforces.com/contest/277/problem/A

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;
 const int N = 102;
 vector < int > g[N];
 int vis[N];
 void dfs(int u) {
   vis[u] = 1;
   for (int i = 0; i < g[u].size(); i++) {
     int v = g[u][i];
     if (vis[v] == 0)
       dfs(v);
   }
 }

 int main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   int n, m, a, b, i, j, z_cnt = 0;
   int lng[N];
   cin >> n >> m;
   for (int i = 1; i <= N; i++) {
     vis[i] = 0;
     lng[i] = 0;
   }
   int nl;
   for (i = 1; i <= n; i++) {
     cin >> nl;
     if (nl == 0)
       z_cnt++;
     else {

       cin >> a;
       lng[a] = 1;
     }
     for (j = 2; j <= nl; j++) {
       cin >> b;
       g[a].push_back(b);
       g[b].push_back(a);
       lng[b] = 1;
     }
   }
   int ans = 0;
   for (i = 1; i <= m; i++) {
     if (vis[i] == 0 && lng[i] == 1) {
       ans++;
       dfs(i);
     }
   }
   ans--;
   cout << max(0, ans) + z_cnt << endl;
 }




No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

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

Powered by Blogger.