qwerty29's blog

By qwerty29, history, 8 months ago, In English

Problem statement: https://cses.fi/problemset/task/1130/
You are given a tree consisting of n nodes.
A matching is a set of edges where each node is an endpoint of at most one edge. What is the maximum number of edges in a matching?

My Approach:
1) Run BFS to find the bipartite partition for the given tree.
2) We iterate over the number of nodes in the maximum of the above sets.
3) Since every node in this set will have a corresponding node in the other set we can choose this node edge in our answer.
4) while taking the nodes in this set we have to make sure that its neighboring node is only visited once i.e. other nodes in this set cannot share a node in the other set.

#include <bits/stdc++.h>
#define all(v) (v).begin(),(v).end()
using namespace std;

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n; cin >> n;
    vector<vector<int> > graph(n+1);
    
    for(int i=0;i<n-1;++i){
        int a,b; cin >> a >> b;
        graph[a].push_back(b);
        graph[b].push_back(a);
    }

    queue<int> q;
    vector<int> color(n+1);
    vector<int> visited(n+1,0);

    q.push(1);
    color[1] = 0;
    visited[1] = 1;
    while(!q.empty()){
        int node = q.front(); q.pop();
        for(int i:graph[node]){
            if(!visited[i]){
                visited[i] = 1;
                color[i] = (color[node]+1)%2;
                q.push(i);
            }
        }
    }
    vector<int> first;
    vector<int> second;
    for(int i=1;i<=n;++i){
        if(color[i]==0) first.push_back(i);
        else second.push_back(i);
    }
    int count = 0;
    fill(all(visited),0);
    if(first.size() < second.size()) swap(first,second);
    for(int i=0;i<first.size();++i) {
        visited[first[i]] = 1;
        for(int it:graph[first[i]]){
            if(!visited[it]){
                visited[it] = 1;
                count++;
                break;
            }
        }
    }
    cout << count << '\n';
    return 0;
}

Am I going in the right direction for this problem since I am new to this concept?
Thank you!.

 
 
 
 
  • Vote: I like it
  • +4
  • Vote: I do not like it

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

It would be great if someone could help out to get solution for this problem

»
7 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

This problem is best solved with DP. First, you should choose a node as a root for the given tree. For the sake of simplicity, you may choose node 1 as your root. Then calculate the following dp[node][taken] = the maximum number of matches you can obtain in the subtree with root node, where taken is 1 if you included node in a matching, 0 if not. The recurrence is:

dp[node][0] = sum(max(dp[son][0], dp[son][1])) where son is a direct son of node

dp[node][1] = max(1 + dp[son][0] + sum(max(dp[son2][0], dp[son2][1])) where son2 is a son of node, and son2 != son) where son is a son of node.

Feel free to ask me if you have further questions on this.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    thank you! very helpful

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    @popabogdannnn can you show your code? My implementation failed based on your idea.

  • »
    »
    3 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I implemented this solution before coming here but getting 3 test cases wrong including 1 TLE, but I am more concerned about WA right now. Can someone spot out the error in mine?

    #include <iostream>
    #include <map>
    #include <vector>
    #define pb push_back
    
    using namespace std;
    
    map<int, vector<int>> m;
    vector<bool> visited;
    vector<vector<int>> dp;
    
    int countMatches(int currNode, int include) {
        if(dp[currNode][include] > 0) return dp[currNode][include];
    
        /*
         * If include  = 1, we include the current node else we exclude it.
         */
        dp[currNode][include] = 0;
        visited[currNode] = true;
        if(include == 0) {
            for(int i: m[currNode]) {
                if(!visited[i]) {
                    if(m[i].size() > 1) dp[currNode][include] += max(countMatches(i, 0), countMatches(i, 1));
                }
            }
        }
    
        else {
            for(int i: m[currNode]) {
                if(visited[i]) continue;
                int k = 1 + (m[i].size() > 1? countMatches(i, 0): 0);
                for(int j: m[currNode]) {
                    if(j == i) continue;
                    if(!visited[j]) if(m[j].size() > 1) k += max(countMatches(j, 0), countMatches(j, 1));
                }
    
                dp[currNode][include] = max(dp[currNode][include], k);
            }
        }
        visited[currNode] = false;
        return dp[currNode][include];
    
    }
    
    int initiater() {
        return max(countMatches(1, 1), countMatches(1, 0));
    }
    
    int main() {
        int n;
        cin >> n;
        int x, y;
        dp = vector<vector<int>>(n+1, vector<int>(2, -1));
        visited = vector<bool>(n, false);
        for(int i=0; i<n-1; i++) {
            cin >> x >> y;
            m[x].pb(y);
            m[y].pb(x);
        }
    
        cout << initiater();
    }
    
  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    dp[node][0] = sum( dp[son][1] ) also works. Why is this greedy decision working?

»
6 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

You are not going in right direction. You need to use a maximum matching algorithm like hopkraft_karp. Your algo will fail in cases like this:

4
1 3
1 4
2 3

if you algo chooses 1 3

Here is my solution using Hopkraft_karp

Code Maximum Matching
»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

One of solutions that I found searching online, Maybe related to what @popabogdannn said ?

#include <iostream>
#include <vector>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;

int n, a, b;
vi dp1, dp2;
vvi g;

void dfs(int u, int p) {
  for (auto z:g[u]) {
    if (z == p) continue;
    dfs(z, u);
    dp2[u] += max(dp1[z], dp2[z]);
  }
  for (auto z:g[u]) {
    if (z == p) continue;
    dp1[u] = max(dp1[u], 1 + dp2[u] + dp2[z] - max(dp1[z], dp2[z]));
  }
}

int main() {
  cin >> n;
  dp1 = vi(n);
  dp2 = vi(n);
  g = vvi(n);
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--; b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0, 0);
  cout << max(dp1[0], dp2[0]) << endl;
}