about summary refs log tree commit diff
path: root/usth/MATH2.3/3/st-dfs.cc
blob: 89814dd85d894c2218ae059bad796a6c9a003e28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <unordered_map>

using namespace std;

void
dfs (unordered_map<size_t, unordered_map<size_t, int>>& graph, 
     unordered_map<size_t, int>& been, size_t current)
{
  been[current] = 1;
  for (auto const& [i, b] : graph[current])
    if (b > 0)
      if (been[i])
        graph[current][i] = graph[i][current] = 0;
      else
        {
          graph[current][i] = graph[i][current] = -1;
          dfs (graph, been, i);
        }
}

int
main ()
{
  size_t n;
  unordered_map<size_t, unordered_map<size_t, int>> edges;

  cin >> n;
  for (size_t i = 0; i < n; ++i)
    for (size_t j = 0; j < n; ++j)
      {
        cin >> edges[i][j];
        edges[j][i] = edges[i][j];
      }

  unordered_map<size_t, unordered_map<size_t, int>> st;
  unordered_map<size_t, int> been;
  dfs (edges, been, 0);

  for (auto const& [i, m] : edges)
    for (auto const& [j, b] : m)
      if (b && i <= j)
        cout << i << " " << j << endl;
}