Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Vet: Who 's next?

Pac: That would be me


Vet: Well, come into the office

*Inside the office*


Vet: Tell me, what's wrong with Spike?
Pac: He hasn't been very appetizing lately.
Vet: I see, apart from that, hasn't Spike seemed more tired than usual?
Pac: No doctor, he is as active as usual.
Vet: And has the amount of water he consumes been affected?
Pac: Neither, only the amount of food he eats has decreased.
Vet: Are you still buying the food that was recommended?
Pac: That's right doctor, that's why I come to you, because I don't know what to do.
Vet: Well, you can see that Spike is not sick, maybe he is just tired of eating the same food
all the time. Try to change the flavor of what he eats, either by changing the product or
adding something else he can eat and you can give him a treat when he finishes his bowl.
Pac: Okay doctor, I will do that, thank you.
Vet: No problem, let's hope Spike gets his appetite back, all the best.

#include <iostream>
#include <vector>
#include <bitset>
#include <numeric>
#include <fstream>

class UnionFind {
private:
std::vector<int> parent;
std::vector<int> rank;

public:
UnionFind(int n) {
parent.resize(n);
rank.resize(n, 0);
std::iota(parent.begin(), parent.end(), 0);
}

int find(int x) {
if (x != parent[x])
parent[x] = find(parent[x]);
return parent[x];
}

void unionNodes(int x, int y) {


int rootX = find(x);
int rootY = find(y);
if (rootX == rootY)
return;
if (rank[rootX] > rank[rootY])
parent[rootY] = rootX;
else if (rank[rootX] < rank[rootY])
parent[rootX] = rootY;
else {
parent[rootY] = rootX;
rank[rootX]++;
}
}

std::vector<int> getClusters() {
std::vector<int> clusters;
for (int i = 0; i < parent.size(); i++) {
if (i == parent[i])
clusters.push_back(i);
}
return clusters;
}
};

std::vector<std::vector<int>> generateDistances(int n_bits, const std::vector<std::string>&


data) {
std::vector<std::vector<int>> distances(data.size(), std::vector<int>(data.size(), 0));

for (int i = 0; i < data.size(); i++) {


for (int j = i + 1; j < data.size(); j++) {
int dist = 0;
for (int k = 0; k < n_bits; k++) {
if (data[i][k] != data[j][k])
dist++;
}
distances[i][j] = dist;
distances[j][i] = dist;
}
}

return distances;
}

int solveP2(const std::vector<std::string>& data, int n_bits) {


std::vector<std::vector<int>> distances = generateDistances(n_bits, data);

std::vector<int> zeroDistances;
std::vector<std::pair<int, int>> oneDistances;
std::vector<std::pair<int, int>> twoDistances;

for (int i = 0; i < data.size(); i++) {


for (int j = i + 1; j < data.size(); j++) {
if (distances[i][j] == 0) {
zeroDistances.push_back(i);
zeroDistances.push_back(j);
} else if (distances[i][j] == 1) {
oneDistances.emplace_back(i, j);
} else if (distances[i][j] == 2) {
twoDistances.emplace_back(i, j);
}
}
}

UnionFind uf(data.size());

for (int i : zeroDistances) {


uf.unionNodes(zeroDistances[0], i);
}

for (const auto& edge : oneDistances) {


uf.unionNodes(edge.first, edge.second);
}

for (const auto& edge : twoDistances) {


uf.unionNodes(edge.first, edge.second);
}

return uf.getClusters().size();
}

int main() {
std::string FILE = R"(C:\Users\Coshita\Downloads\Cluster\clustering2.txt)";
std::ifstream fp(FILE);
int n_nodes, n_bits;
fp >> n_nodes >> n_bits;

std::vector<std::string> data;
for (int i = 0; i < n_nodes; i++) {
std::string row;
fp >> row;
data.push_back(row);
}

int solved = solveP2(data, n_bits);

std::cout << "P2 Solution: " << solved << std::endl;


return 0;
}
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

int main() {
string file = R"(C:\Users\Coshita\Downloads\Mochila\knapsack1.txt)";
ifstream fp(file);

int W, n;
fp >> W >> n;

vector<int> v(n);
vector<int> w(n);

for (int i = 0; i < n; i++) {


fp >> v[i] >> w[i];
}

vector<vector<int>> A(n + 1, vector<int>(W + 1, 0));

for (int i = 1; i <= n; i++) {


for (int x = 0; x <= W; x++) {
if (w[i - 1] <= x) {
A[i][x] = max(A[i - 1][x], A[i - 1][x - w[i - 1]] + v[i - 1]);
} else {
A[i][x] = A[i - 1][x];
}
}
}

cout << A[n][W] << endl;

return 0;
}
#include <iostream>
#include <vector>
#include <unordered_map>
#include <fstream>

using namespace std;

unordered_map<string, int> cache;

int knap(int i, int _w, const vector<int>& v, const vector<int>& w) {


string key = to_string(i) + "-" + to_string(_w);
if (cache.find(key) != cache.end()) {
return cache[key];
}

if (i == 0) {
cache[key] = 0;
return 0;
}

if (_w >= w[i]) {


int a1 = knap(i - 1, _w - w[i], v, w) + v[i];
int a2 = knap(i - 1, _w, v, w);
cache[key] = max(a1, a2);
} else {
cache[key] = knap(i - 1, _w, v, w);
}

return cache[key];
}

int main() {
string file = R"(C:\Users\Coshita\Downloads\Mochila\knapsack2.txt)";
ifstream fp(file);

int W, n;
fp >> W >> n;

vector<int> v(n);
vector<int> w(n);

for (int i = 0; i < n; i++) {


fp >> v[i] >> w[i];
}

int result = knap(n - 1, W, v, w);


cout << result << endl;

return 0;
}

You might also like