Simple BFS and DFS with priority queue
Method 1- BFS\n# Code\n\nclass Solution {\npublic:\n    \n    int maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {\n        int n=vals.size();
# Method 1- BFS\n# Code\n```\nclass Solution {\npublic:\n    \n    int maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {\n        int n=vals.size();\n        vector<int>adj[n];\n        for(auto it:edges){\n            adj[it[0]].push_back(it[1]);\n            adj[it[1]].push_back(it[0]);\n        }\n \n         int ans=INT_MIN;\n         for(int i=0;i<n;i++){\n             priority_queue<int>pq;\n             int temp=0;\n             for(auto it:adj[i]){\n                 if(vals[it]>0)pq.push(vals[it]);\n             }\n             int sz=pq.size();\n             for(int j=0;j<min(k,sz);j++){\n                 temp+=pq.top();\n                 pq.pop();\n             }\n             temp+=vals[i];\n             ans=max(ans,temp);\n\n            \n         }\n         return ans;\n        \n    }\n\n```\n# Method 2- DFS\n# Code\n```\nclass Solution {\npublic:\n    int dfs(vector<int>adj[], vector<bool>&vis,int node, int k , vector<int>&vals){\n        vis[node]=true;\n        priority_queue<int>pq;\n        int res=INT_MIN;\n        for(auto it:adj[node]){\n            if(vals[it]>0)pq.push(vals[it]);\n            if(vis[it]==false){\n                res=max(res,dfs(adj ,vis, it, k, vals ));\n            }\n        }\n        int sum=0;\n        int sz=pq.size();\n        for(int i=0;i<min(k,sz);i++){\n            sum+=pq.top();\n            pq.pop();\n        }\n        sum+=vals[node];\n        return max(res,sum);\n    }\n    int maxStarSum(vector<int>& vals, vector<vector<int>>& edges, int k) {\n        int n=vals.size();\n        vector<int>adj[n];\n        for(auto it:edges){\n            adj[it[0]].push_back(it[1]);\n            adj[it[1]].push_back(it[0]);\n        }\n        \n        int ans=INT_MIN;\n\n        vector<bool>vis(n,false);\n        for(int i=0;i<n;i++){\n            if(vis[i]==false){\n                ans=max(ans,dfs(adj,vis,i,k,vals));\n            }\n        }\n        return ans;\n      \n     \n    }\n};\n```
['Depth-First Search', 'Breadth-First Search', 'Heap (Priority Queue)', 'C++']