[InterviewBit] Maximum Edge Removal

Maximum Edge Removal

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int dfs(vector<vector<int>>& adj, int u, int par, int& res) {
int now = 1;
for(auto& v : adj[u]) {
if(v == par) continue;
int sub = dfs(adj,v,u,res);
if(sub % 2 == 0) res += 1;
else now += sub;
}
return now;
}

int Solution::solve(int A, vector<vector<int>> &B) {
vector<vector<int>> adj(A + 1);
for(auto b : B) {
int u = b[0], v = b[1];
adj[u].push_back(v);
adj[v].push_back(u);
}
int res = 0;
dfs(adj,1,-1,res);
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/07/PS/interviewbit/maximum-edge-removal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.