Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.37 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <strstream>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. int N=0;
  10. int M=0;
  11. vector < vector <int> > Digraph;
  12. vector<char> used;
  13. int Time;
  14. vector<int> tin, fup;
  15. vector<int> br;
  16. int count = 0;
  17.  
  18.  
  19.  
  20. void IsBridge(int from, int to){
  21.     count++;
  22.     int i = 0;
  23.         bool l = true;
  24.         while ((i < Digraph[from].size()) && (l)) {
  25.         if(Digraph[from][i] == to){
  26.             br.push_back(Digraph[from][i]);
  27.                         l = false;
  28.                 }
  29.                 i++;
  30.     }
  31. }
  32.  
  33. void dfs (int from, int p = 0) {
  34.         used[from] = true;
  35.         tin[from] = fup[from] = Time;
  36.     Time++;
  37.         for (int i=0; i < Digraph[from].size(); i++) {
  38.                 int to = Digraph[from][i];
  39.                 if (to == p)  continue;
  40.                 if (used[to]) fup[from] = min (fup[from], tin[to]);
  41.                 else {
  42.                         dfs (to, from);
  43.                         fup[from] = min (fup[from], fup[to]);
  44.                         if (fup[to] > tin[from])
  45.                                 IsBridge(from,to);
  46.                 }
  47.         }
  48. }
  49.  
  50. int main () {
  51.     freopen("bridges.in","r",stdin);
  52.         freopen("bridges.out","w",stdout);
  53.  
  54.     cin >> N >> M;
  55.  
  56.     for(int i = 1; i <= M; i++){
  57.         int a,b;
  58.         cin >> a >> b;
  59.         Digraph[a].push_back(b);
  60.         Digraph[b].push_back(a);
  61.     }
  62.     Time = 0;
  63.  
  64.     dfs(1);
  65.  
  66.     cout << count << "\n";
  67.  
  68.     sort(br.begin(), br.end());
  69.  
  70.     for (int i=0; i < br.size(); i++) {
  71.         cout << br[i] << " ";
  72.     }
  73.  
  74.     return 0;
  75. }