fork download
  1. class Solution {
  2. private:
  3. vector<vector<int>> adj;
  4. vector<int> g;
  5. long long ans;
  6.  
  7. // Returns count of nodes with target group in subtree
  8. int dfs(int u, int p, int tar, long long tot) {
  9. // Count current node if it matches
  10. int cnt = (g[u] == tar) ? 1 : 0;
  11.  
  12. // Visit all children
  13. for (int v : adj[u]) {
  14. if (v == p) continue;
  15.  
  16. int sub = dfs(v, u, tar, tot);
  17.  
  18. // Edge cost = one side * other side
  19. long long rem = tot - sub;
  20. ans += sub * rem;
  21.  
  22. cnt += sub;
  23. }
  24.  
  25. return cnt;
  26. }
  27.  
  28. public:
  29. long long interactionCosts(int n, vector<vector<int>>& edges, vector<int>& group) {
  30. adj.resize(n);
  31. g = group;
  32. ans = 0;
  33.  
  34. // Build tree
  35. for (auto &e : edges) {
  36. adj[e[0]].push_back(e[1]);
  37. adj[e[1]].push_back(e[0]);
  38. }
  39.  
  40. // Count each group size
  41. vector<int> sz(21, 0);
  42. for (int x : group) {
  43. sz[x]++;
  44. }
  45.  
  46. // Process each group with 2+ nodes
  47. for (int i = 1; i <= 20; i++) {
  48. if (sz[i] >= 2) {
  49. dfs(0, -1, i, sz[i]);
  50. }
  51. }
  52.  
  53. return ans;
  54. }
  55. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:3:5: error: ‘vector’ does not name a type
     vector<vector<int>> adj;
     ^~~~~~
prog.cpp:4:5: error: ‘vector’ does not name a type
     vector<int> g;
     ^~~~~~
prog.cpp:29:39: error: ‘vector’ has not been declared
     long long interactionCosts(int n, vector<vector<int>>& edges, vector<int>& group) {
                                       ^~~~~~
prog.cpp:29:45: error: expected ‘,’ or ‘...’ before ‘<’ token
     long long interactionCosts(int n, vector<vector<int>>& edges, vector<int>& group) {
                                             ^
prog.cpp: In member function ‘int Solution::dfs(int, int, int, long long int)’:
prog.cpp:10:20: error: ‘g’ was not declared in this scope
         int cnt = (g[u] == tar) ? 1 : 0;
                    ^
prog.cpp:13:22: error: ‘adj’ was not declared in this scope
         for (int v : adj[u]) {
                      ^~~
prog.cpp: In member function ‘long long int Solution::interactionCosts(int, int)’:
prog.cpp:30:9: error: ‘adj’ was not declared in this scope
         adj.resize(n);
         ^~~
prog.cpp:31:9: error: ‘g’ was not declared in this scope
         g = group;
         ^
prog.cpp:31:13: error: ‘group’ was not declared in this scope
         g = group;
             ^~~~~
prog.cpp:35:24: error: ‘edges’ was not declared in this scope
         for (auto &e : edges) {
                        ^~~~~
prog.cpp:41:9: error: ‘vector’ was not declared in this scope
         vector<int> sz(21, 0);
         ^~~~~~
prog.cpp:41:16: error: expected primary-expression before ‘int’
         vector<int> sz(21, 0);
                ^~~
prog.cpp:42:22: error: unable to deduce ‘auto&&’ from ‘group’
         for (int x : group) {
                      ^~~~~
prog.cpp:43:13: error: ‘sz’ was not declared in this scope
             sz[x]++;
             ^~
prog.cpp:48:17: error: ‘sz’ was not declared in this scope
             if (sz[i] >= 2) {
                 ^~
stdout
Standard output is empty