Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.
Locked until you have predicted 3 outputs correctly.
| 1 | int root(vector<int>& owner, int x) { |
| 2 | |
| 3 | while (owner[x] != x) { |
| 4 | owner[x] = owner[owner[x]]; |
| 5 | x = owner[x]; |
| 6 | } |
| 7 | |
| 8 | return x; |
| 9 | } |
| 10 | |
| 11 | int solution(int n, const vector<vector<int>>& edges) { |
| 12 | |
| 13 | vector<int> owner(n); |
| 14 | |
| 15 | for (int i = 0; i < n; i++) { |
| 16 | owner[i] = i; |
| 17 | } |
| 18 | |
| 19 | int groups = n; |
| 20 | |
| 21 | for (const auto& e : edges) { |
| 22 | |
| 23 | int a = root(owner, e[0]); |
| 24 | int b = root(owner, e[1]); |
| 25 | |
| 26 | if (a == b) { |
| 27 | continue; |
| 28 | } |
| 29 | |
| 30 | owner[a] = b; |
| 31 | groups--; |
| 32 | } |
| 33 | |
| 34 | return groups; |
| 35 | } |
Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.