Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- enum EuropeCountriesNum {
- AD,
- AL,
- AM,
- AT,
- BA,
- BE,
- BG,
- BY,
- CH,
- CY,
- CZ,
- DE,
- DK,
- EE,
- ES,
- FI,
- FR,
- GE,
- GR,
- HR,
- HU,
- IE,
- IS,
- IT,
- LI,
- LT,
- LU,
- LV,
- MC,
- MD,
- ME,
- MK,
- MT,
- NL,
- NO,
- PL,
- PT,
- RO,
- RS,
- RU,
- SE,
- SI,
- SK,
- SM,
- TR,
- UA,
- UK,
- VA,
- XK
- };
- const char* EuropeCountriesName[] = {"AD", "AL", "AM", "AT", "BA", "BE", "BG", "BY", "CH", "CY", "CZ", "DE", "DK", "EE", "ES",
- "FI", "FR", "GE", "GR", "HR", "HU", "IE", "IS", "IT", "LI", "LT", "LU", "LV", "MC", "MD",
- "ME", "MK", "MT", "NL", "NO", "PL", "PT", "RO", "RS", "RU", "SE", "SI", "SK", "SM", "TR",
- "UA", "UK", "VA", "XK"};
- class Graph {
- private:
- std::vector<std::vector<size_t>> graph_;
- std::vector<std::vector<int>> distances_;
- std::vector<std::vector<int>> connectivity_components_;
- std::vector<int> center_;
- std::vector<int> largest_connectivity_component_;
- std::vector<std::vector<int>> graph_largest_connectivity_component_;
- std::vector<std::vector<int>> matrix_;
- size_t size_;
- int rad_;
- int delta_max_;
- int delta_min_;
- const int MAXN = 100; // maximum number of vertices
- const int INF = 0x3f3f3f3f;
- int match[100];
- bool visited[100];
- int parent[100];
- int capacity[100][100];
- private:
- void floyd_warshall() {
- int n = graph_.size();
- // std::vector<std::vector<int>> distances_(n, std::vector<int>(n));
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (find(graph_[i].begin(), graph_[i].end(), j) != graph_[i].end()) {
- distances_[i][j] = 1;
- } else {
- distances_[i][j] = INT_MAX;
- }
- }
- }
- for (int k = 0; k < n; k++) {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (i == j) {
- continue;
- }
- if (distances_[i][k] != INT_MAX && distances_[k][j] != INT_MAX) {
- distances_[i][j] = std::min(distances_[i][j], distances_[i][k] + distances_[k][j]);
- }
- }
- }
- }
- }
- void find_connectivity_components() {
- std::vector<bool> visited(size_, false);
- std::vector<int> vec_temp;
- std::queue<int> q;
- for (int v = 0; v < size_; v++) {
- if (visited[v]) {
- continue;
- }
- q.push(v);
- visited[v] = true;
- vec_temp.push_back(v);
- while (!q.empty()) {
- int cur = q.front();
- q.pop();
- for (auto to: graph_[cur]) {
- if (!visited[to]) {
- q.push(to);
- vec_temp.push_back(to);
- visited[to] = true;
- }
- }
- }
- connectivity_components_.push_back(vec_temp);
- vec_temp.clear();
- }
- }
- public:
- Graph(size_t size) : size_(size), rad_(-1), delta_max_(-1), delta_min_(-1) {
- graph_.resize(size);
- distances_ = std::vector<std::vector<int>>(size_, std::vector<int>(size_));
- }
- void add_edge(size_t u, size_t v) {
- graph_[u].push_back(v);
- graph_[v].push_back(u);
- // matrix_[u][v] = 1;
- // matrix_[v][u] = 1;
- }
- void pre_commands() {
- floyd_warshall();
- find_connectivity_components();
- int idx = 0;
- int size_temp = 0;
- for (int i = 0; i < connectivity_components_.size(); i++) {
- if (connectivity_components_[i].size() > size_temp) {
- idx = i;
- size_temp = connectivity_components_[i].size();
- }
- }
- largest_connectivity_component_ = connectivity_components_[idx];
- graph_largest_connectivity_component_.resize(largest_connectivity_component_.size());
- std::unordered_map<int, int> vertex_change_map;
- std::vector<std::string> names_(largest_connectivity_component_.size());
- for (int i = 0; i < largest_connectivity_component_.size(); i++) {
- vertex_change_map[largest_connectivity_component_[i]] = i;
- names_[i] = EuropeCountriesName[largest_connectivity_component_[i]];
- }
- std::vector<int> temp_vec;
- for (int i = 0; i < largest_connectivity_component_.size(); i++) {
- for (int j : graph_[largest_connectivity_component_[i]]) {
- temp_vec.push_back(vertex_change_map[j]);
- }
- graph_largest_connectivity_component_[i] = temp_vec;
- temp_vec.clear();
- }
- for (int i = 0; i < graph_largest_connectivity_component_.size(); i++) {
- for (auto j : graph_largest_connectivity_component_[i]) {
- std::cout << names_[i] << " -- " << names_[j] << '\n';
- }
- }
- const size_t size_new = graph_largest_connectivity_component_.size();
- matrix_ = std::vector<std::vector<int>>(size_new, std::vector<int>(size_new));
- for(int i = 0; i < size_new; i++) {
- for (auto j : graph_largest_connectivity_component_[i]) {
- matrix_[i][j] = 1;
- matrix_[j][i] = 1;
- }
- }
- }
- int dist(size_t u, size_t v) {
- return distances_[u][v];
- }
- int e(size_t v) {
- int answer = 0;
- for (auto u : largest_connectivity_component_) {
- if (u != v)
- answer = std::max(answer, dist(u, v));
- }
- return answer;
- }
- int rad() {
- if (rad_ != -1) {
- return rad_;
- }
- int answer = INT_MAX;
- for (auto v : largest_connectivity_component_) {
- answer = std::min(answer, e(v));
- }
- rad_ = answer;
- return answer;
- }
- int diam() {
- int answer = INT_MIN;
- for (auto v : largest_connectivity_component_) {
- answer = std::max(answer, e(v));
- }
- return answer;
- }
- std::vector<int>& center() {
- if (!center_.empty()) {
- return center_;
- }
- for (auto v : largest_connectivity_component_) {
- if (e(v) == rad()) {
- center_.push_back(v);
- }
- }
- return center_;
- }
- int deg(size_t v) {
- return graph_[v].size();
- }
- int delta_max() {
- if (delta_max_ != -1) {
- return delta_max_;
- }
- int answer = INT_MIN;
- for (auto v : largest_connectivity_component_) {
- answer = std::max(answer, deg(v));
- }
- delta_max_ = answer;
- return answer;
- }
- int delta_min() {
- if (delta_min_ != -1) {
- return delta_min_;
- }
- int answer = INT_MAX;
- for (auto v : largest_connectivity_component_) {
- answer = std::min(answer, deg(v));
- }
- delta_min_ = answer;
- return answer;
- }
- void print_edges() {
- for (int i = 0; i < graph_largest_connectivity_component_.size(); i++) {
- for (int j : graph_largest_connectivity_component_[i]) {
- std::cout << i << ' ' << j << '\n';
- }
- }
- }
- };
- int main() {
- const size_t kNumberOfCountriesInEurope = 49;//49;
- const size_t kNumberOfEdgesInEuropeGraph = 91;//91;
- Graph Europe(kNumberOfCountriesInEurope);
- for (int i = 0; i < kNumberOfEdgesInEuropeGraph; i++) {
- int u;
- int v;
- std::cin >> u >> v;
- Europe.add_edge(u, v);
- }
- Europe.pre_commands();
- // std::cout << "-----------------------------\n";
- // std::cout << "rad(G) = " << Europe.rad() << '\n';
- // std::cout << "diam(G) = " << Europe.diam() << '\n';
- // std::cout << "girth(G) = " << ' ' << '\n';
- // std::cout << "center(G) = {";
- // for (auto i : Europe.center()) {
- // std::cout << i << ' ';// EuropeCountriesName[i] << ' ';
- // }
- // std::cout << "}\n";
- // Europe.print_edges();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement