You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.3 KiB
C++
128 lines
3.3 KiB
C++
2 years ago
|
#pragma once
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <vector>
|
||
|
#include <string>
|
||
|
#include <unordered_map>
|
||
|
#include <unordered_set>
|
||
|
#include <stdint.h>
|
||
|
#include <numeric>
|
||
|
#include <bits/stdc++.h>
|
||
|
#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<string, vector<Edge>> 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<Edge> star) {
|
||
|
auto it = star.begin();
|
||
|
auto v = AdjList[(*it).to];
|
||
|
unordered_set<string> acc;
|
||
|
for (auto& e : v) {
|
||
|
acc.insert(e.to);
|
||
|
}
|
||
|
|
||
|
it++;
|
||
|
|
||
|
for (; it != star.end(); it++) {
|
||
|
auto v = AdjList[(*it).to];
|
||
|
unordered_set<string> s;
|
||
|
for (auto& e : v) {
|
||
|
s.insert(e.to);
|
||
|
}
|
||
|
acc = intersectSets(acc,s);
|
||
|
}
|
||
|
|
||
|
return acc.size() == 1;
|
||
|
}
|
||
|
|
||
|
vector<vector<Edge>> findSol(int c, int k) {
|
||
|
vector<vector<Edge>> Sol;
|
||
|
|
||
|
vector<string> 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<int> indices(k);
|
||
|
iota(indices.begin(), indices.end(), 0);
|
||
|
do {
|
||
|
|
||
|
vector<Edge> 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<vector<Edge>> Sol) {
|
||
|
for (auto& edges : Sol) {
|
||
|
cout << edges[0].from << ":" << " ";
|
||
|
for (auto& edge : edges) {
|
||
|
cout << edge.to << " ";
|
||
|
}
|
||
|
cout << endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
};
|