Sort by

recency

|

28 Discussions

|

  • + 1 comment

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Crab Graphs Problem Solution

  • + 0 comments

    Here is the solution of Crab Graphs Click Here

  • + 2 comments

    Python 3: solution without flow algorithm

    def crabGraphs(n, t, graph):
        cnn={x:[] for x in range(1,n+1)}
        for u,v in graph:
            cnn[u].append(v)
            cnn[v].append(u)
        nodes=set()
        for u in sorted(cnn, key=lambda s:len(cnn[s]),reverse=True):
            if u not in nodes and len(cnn[u])>=t:
                nodes.add(u)
        for u in sorted(cnn, key=lambda s:len(cnn[s]),reverse=True):
            feetofu=0
            for v in cnn[u]:
                if v not in nodes and feetofu<t:
                    nodes.add(v)
                    feetofu+=1
        return len(nodes)
    
  • + 1 comment

    can anybody explain how for this (first test in testcase #1) 50 4 4 46 44 46 10 20 16 43 21 the answer will be 7??? Obviously there is no crabs with 4 legs in this graph.

  • + 1 comment

    c++ soution

    include

    include

    include

    using std::vector; using std::deque; using Matrix=vector>;

    int residual_capacity(const Matrix &cap, const Matrix &flow, int from, int to){ if(cap[from][to]) return cap[from][to]-flow[from][to]; else if(cap[to][from]) return flow[to][from]; else return 0; }

    vector residual_path(const Matrix &cap, const Matrix &flow){ const int no_parent=-2, unvisited=-1; const int source=cap.size()-2,sink=source+1; vector parent(sink+1,unvisited); parent[source]=no_parent; deque bfs; vector path; bfs.push_back(source); while(!bfs.empty()){ int i=bfs.front(); bfs.pop_front(); for(int j=0;j

    Matrix max_flow(const Matrix &cap){ const int source=cap.size()-2,sink=source+1; Matrix flow(sink+1,vector(sink+1,0)); auto path=residual_path(cap,flow); while(!path.empty()){ int min_cap=cap.size(); for(int j=0;j

    int solve(Matrix &cap){ const int source=cap.size()-2,nnode=source/2; auto flow=max_flow(cap); int total=0; for(int i=0;i

    void input_case(){ int nnode=0, nfeet=0, nedge=0; std::cin >> nnode >> nfeet >> nedge; const int source=2*nnode, sink=source+1; Matrix cap(sink+1,vector(sink+1,0)); while(nedge--){ int i=0, j=0; std::cin >> i >> j; --i; --j; cap[2*i][2*j+1]=1; cap[2*j][2*i+1]=1; ++cap[source][2*i]; ++cap[source][2*j]; } for(int i=0;infeet){ cap[source][2*i]=nfeet; } } std::cout << solve(cap) << '\n'; }

    int main(){ int ncase=0; std::cin >> ncase; while(ncase--) input_case(); }