#pragma once #include #include #include #include #include #include #include #include #include "aux.cpp" using namespace std; struct Edge { string from, to; int weight; // void print() { // cout << from << " --> " << to << " w: " << weight << endl; // } }; bool compareEdge(Edge e1, Edge e2) { return (e1.weight > e2.weight); } class UndirectedWeightedGraph { private: void addWeightedEdge(string from, string to, int weight) { for (Edge& edge : AdjList[from]) { if (to == edge.to) { edge.weight += weight; return; } } Edge newEdge; newEdge.from = from; newEdge.to = to; newEdge.weight = weight; AdjList[from].push_back(newEdge); } public: unordered_map> AdjList; void addEdge(string node1, string node2) { addWeightedEdge(node1, node2, 1); addWeightedEdge(node2, node1, 1); } // void print() { // for (auto& pair : AdjList) { // for (Edge& edge : pair.second) { // edge.print(); // } // } // } bool checkComb(vector star) { auto it = star.begin(); auto v = AdjList[(*it).to]; unordered_set acc; for (auto& e : v) { acc.insert(e.to); } it++; for (; it != star.end(); it++) { auto v = AdjList[(*it).to]; unordered_set s; for (auto& e : v) { s.insert(e.to); } acc = intersectSets(acc,s); } return acc.size() == 1; } vector> findSol(int c, int k) { vector> Sol; vector keys = getShuffleKeys(AdjList); for (auto& key : keys) { auto node = AdjList[key]; if (c == 0) break; if (node.size() >= k) { sort(node.begin(), node.end(), compareEdge); vector indices(k); iota(indices.begin(), indices.end(), 0); do { vector subV; for (int i : indices) subV.push_back(node[i]); if (checkComb(subV)) { c--; Sol.push_back(subV); break; } } while(!nextComb(indices, node.size())); } } return Sol; } void printSol(vector> Sol) { for (auto& edges : Sol) { cout << edges[0].from << ":" << " "; for (auto& edge : edges) { cout << edge.to << " "; } cout << endl; } } };