AIM – A crab is an undirected graph which has two kinds of vertices: 1 head, and K feet , and
exactly K edges which join the head to each of the feet.( 1 <= K <= T, where T is given)
Given an undirected graph, you have to find in it some vertex-disjoint subgraphs where each one
is a crab . The goal is to select those crabs in such a way that the total number of vertices covered
by them is maximized.
CODE-
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define source (0)
#define target (1)
int capacity[300][300];
int flow[300][300];
int max_flow(int n, int s, int t) {
memset(flow, 0, sizeof(flow));
int max_flow = 0;
while (true) {
bool visited[300];
memset(visited, 0 , sizeof(visited));
int parent[300];
queue<int> bfs;
bfs.push(s);
visited[s] = true;
parent[s] = -1;
while (!bfs.empty()) {
int curr = bfs.front();
bfs.pop();
for (int i = 0; i < n; ++i) {
if (!visited[i] && capacity[curr][i] > flow[curr][i] -
flow[i][curr]) {
bfs.push(i);
parent[i] = curr;
visited[i] = true;
}
}
}
if (!visited[t]) { break; }
int path_flow = 0x7FFFFFFF;
for (int v = t; v != s; v = parent[v])
{
int u = parent[v];
path_flow = min(path_flow, capacity[u][v] - flow[u][v] + fl
ow[v][u]);
}
// update residual capacities of the edges and reverse edges
// along the path
for (int v = t; v != s; v = parent[v])
{
int u = parent[v];
flow[u][v] += path_flow;
}
// Add path flow to overall flow
max_flow += path_flow;
}
return max_flow;
}
#define ODD(x) (2 * (x) + 1)
#define EVEN(x) (2 * (x) + 2)
int main() {
/* Enter your code here. Read input from STDIN. Print output to STD
OUT */
int C;
cin >> C;
while (C--) {
memset(capacity, 0, sizeof(capacity));
int N, T, M;
cin >> N >> T >> M;
for (int i = 0; i < M; ++i) {
int v, w;
cin >> v >> w;
capacity[EVEN(v)][ODD(w)] = 1;
capacity[EVEN(w)][ODD(v)] = 1;
}
for (int v = 1; v <= N; ++v) {
capacity[source][EVEN(v)] = T;
capacity[ODD(v)][target] = 1;
}
cout << max_flow(2 * N + 3, source, target) << endl;
}
return 0;
}
OUTPUT: