From a3dd2581ed4847670f81157091016c14ca18803d Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Tue, 14 Jan 2020 18:29:11 +0700 Subject: [usth/MATH2.3] Mathemate Discretely --- usth/MATH2.3/3/graph-is-tree.cc | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 usth/MATH2.3/3/graph-is-tree.cc (limited to 'usth/MATH2.3/3/graph-is-tree.cc') diff --git a/usth/MATH2.3/3/graph-is-tree.cc b/usth/MATH2.3/3/graph-is-tree.cc new file mode 100644 index 0000000..f79564d --- /dev/null +++ b/usth/MATH2.3/3/graph-is-tree.cc @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +using namespace std; + +int +main () +{ + size_t n; + int b; + unordered_map> edges; + + int m = 0; + cin >> n; + for (size_t i = 0; i < n; ++i) + for (size_t j = 0; j < n; ++j) + { + cin >> b; + if (!b || i > j) + continue; + edges[i].insert (j); + edges[j].insert (i); + m++; + } + + if (n - m - 1) + { + cout << "no" << endl; + return 0; + } + + unordered_set s; + int count = -1; + for (auto const& [v, p] : edges) + { + if (s.count (v)) + continue; + count++; + vector lifo {v}; + while (!lifo.empty ()) + { + size_t u = lifo.back (); + lifo.pop_back (); + if (s.count (u)) + continue; + s.insert (u); + for (auto const& w : edges[u]) + if (!s.count (w)) + lifo.push_back (w); + } + } + + cout << (count ? "no" : "yes") << endl; +} -- cgit 1.4.1