C++ · Solution

Cindy Homework

This C++ solution uses disjoint set union for Cindy Homework. Read the reasoning, inspect the code, or try your own test case below.

Graphs & treesDisjoint set unionC++63 lines
Solution045of 248
Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

Disjoint set union

Cindy Homework: maintain connected components with parent representatives, merging sets as relationships are added and querying representatives to test connectivity.

ConnectivityGraphs

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

View exact source file ↗
Implementation

cindy_homework.cpp

C++

    #include <bits/stdc++.h>
     
    using namespace std;
     
    struct DSU {
        vector<int> parent;   
        vector<int> rank;      
     
        DSU(int n) : parent(n + 1), rank(n + 1, 0) {
            iota(parent.begin(), parent.end(), 0);  // parent[i] = i
        }
     
        int find(int x) {                      
            return parent[x] == x ? x : parent[x] = find(parent[x]);
        }
     
        void unite(int x, int y) {              
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) return;      
     
            if (rank[rootX] > rank[rootY]) {    
                parent[rootY] = rootX;
            } else if (rank[rootX] < rank[rootY]) {
                parent[rootX] = rootY;
            } else {                            
                parent[rootY] = rootX;
                rank[rootX]++;                 
            }
        }
    };
     
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
     
        int n, m; // n = people, m = classes
        cin >> n >> m;
     
        DSU dsu(n);
     
        for (int i = 0; i < m; i++) {
            int k; cin >> k;                  
            int first;
            cin >> first;  // representative student
            for (int i = 1; i < k; i++) {       // union with the rest
                int x; 
                cin >> x;
                dsu.unite(first, x);
            }
        }
        //person one is by default infected 
        vector<int> infected;
        int person = dsu.find(1);
        for (int i = 1; i <= n; ++i){
            if (dsu.find(i) == person){
            infected.push_back(i);
        }
      }
        cout << infected.size() << '\n';
        for (int i = 0; i < infected.size(); i++)
            cout << infected[i] << (i + 1 == infected.size() ? '\n' : ' ');
        return 0;
    }
     
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.