Facebook
From Lousy Partdridge, 1 Year ago, written in C++.
Embed
Download Paste or View Raw
Hits: 91
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. struct Edge {
  6.     Edge(int dest, int we) {
  7.         this->dest = dest;
  8.         this->weight = we;
  9.     }
  10.     int dest, weight;
  11. };
  12.  
  13. const int MAXN = 54321;
  14. bool used[MAXN];
  15. int count = 0, tin[MAXN], fup[MAXN];
  16.  
  17. void DfsBriges(int vertex, int parent, std::vector<std::vector<Edge>> &graph, int &answ) {
  18.     used[vertex] = true;
  19.     tin[vertex] = count;
  20.     fup[vertex] = count++;
  21.     for (Edge edge:graph[vertex]) {
  22.         int dest = edge.dest;
  23.         if (dest == parent) {
  24.             continue;
  25.         }
  26.         if (used[dest]) {
  27.             fup[vertex] = std::min(fup[vertex], fup[dest]);
  28.         } else {
  29.             DfsBriges(dest, vertex, graph, answ);
  30.             fup[vertex] = std::min(fup[vertex], fup[dest]);
  31.             if (fup[dest] > tin[vertex]) {
  32.                 answ = std::min(answ, edge.weight);
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. int main() {
  39.     int nn, mm;
  40.     std::cin >> nn >> mm;
  41.     std::vector<std::vector<Edge>> graph(nn + 1);
  42.     for (int i = 0; i < mm; ++i) {
  43.         int source, dest, weight;
  44.         std::cin >> source >> dest >> weight;
  45.         graph[source].push_back(Edge(dest, weight));
  46.         graph[dest].push_back(Edge(source, weight));
  47.     }
  48.     int answ = 1e9;
  49.     DfsBriges(1, -1, graph, answ);
  50.     if (answ < 1e9) {
  51.         std::cout << answ;
  52.     } else {
  53.         std::cout << -1;
  54.     }
  55. }
  56.  

Replies to Untitled rss

Title Name Language When
Re: Untitled Gruff Agouti cpp 1 Year ago.